onNavigationItemSelected 在 NavigationView 中不起作用

onNavigationItemSelected not working in NavigationView

有人可以帮我处理导航抽屉中的片段吗,出于某种原因我无法让它们工作,但所有代码看起来都正确。

Here是源代码的link。

看看你的MainActivity.java

您已经在 MainActivity 中实现了 NavigationView.OnNavigationItemSelectedListener 的回调,如下所示,

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    // blah blah
}

同时检查 setupDrawerContent 方法。

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    return true;
                }
            });
}

在此方法中,您将创建本地 OnNavigationItemSelectedListener

所以您没有使用在 MainActivity 中覆盖的 OnNavigationItemSelectedListener

解决方案是使用 this 作为 setNavigationItemSelectedListener 的参数。通过这样做,您的所有点击都将转到 MainActivityonNavigationItemSelected 而不是转到本地 onNavigationItemSelected.

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(this);
}

也将本地onNavigationItemSelected中的代码移动到MainActivityonNavigationItemSelected中。

所以你的 onNavigationItemSelected 会是这样的,

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    // Handle navigation view item clicks here.
    int id = menuItem.getItemId();
    menuItem.setChecked(true);
    drawerLayout.closeDrawers();

    if (id == R.id.nav_home) {
        // Handle the home action
        Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
    } else if (id == R.id.nav_the_wetlands) {
        Toast.makeText(this, "The Wetlands", Toast.LENGTH_SHORT).show();
        TheWetlandsFragment theWetlandsFragment = new TheWetlandsFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.relativelayout_for_fragment, theWetlandsFragment, theWetlandsFragment.getTag()).commit();
    } else if (id == R.id.nav_the_mistbelt_forests) {
        Toast.makeText(this, "The Mistbelt Forests", Toast.LENGTH_SHORT).show();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

同时更改您的 activity_main_drawer_view.xml 以解决您在导航抽屉中的多选问题,

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_dashboard"
            android:title="Home" />
    </group>

    <item android:title="Information">
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_the_wetlands"
                android:icon="@drawable/ic_event"
                android:title="The Wetlands" />
            <item
                android:id="@+id/nav_the_mistbelt_forests"
                android:icon="@drawable/ic_event"
                android:title="The Mistbelt Forests" />
            <item
                android:id="@+id/nav_the_grasslands"
                android:icon="@drawable/ic_event"
                android:title="The Grasslands" />
        </group>
    </item>

    <item android:title="Quick Go To">
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_accommodation"
                android:icon="@drawable/ic_event"
                android:title="Accommodation" />
            <item
                android:id="@+id/nav_cuisine"
                android:icon="@drawable/ic_forum"
                android:title="Cuisine" />
            <item
                android:id="@+id/nav_leisure_activites"
                android:icon="@drawable/ic_forum"
                android:title="Leisure &amp; Activites" />
            <item
                android:id="@+id/nav_agri_tourism"
                android:icon="@drawable/ic_forum"
                android:title="Agri-tourism" />
            <item
                android:id="@+id/nav_education"
                android:icon="@drawable/ic_forum"
                android:title="Education" />
            <item
                android:id="@+id/nav_arts_crafts_decor"
                android:icon="@drawable/ic_forum"
                android:title="Arts, Crafts &amp; DeCor" />
            <item
                android:id="@+id/nav_selective_shopping"
                android:icon="@drawable/ic_forum"
                android:title="Selective Shopping" />
            <item
                android:id="@+id/nav_for_children"
                android:icon="@drawable/ic_forum"
                android:title="For Children" />
        </group>
    </item>

    <item android:title="Midlands Animals">
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_midlands_birding_checklist"
                android:icon="@drawable/ic_dashboard"
                android:title="Midlands Birding Checklist" />
            <item
                android:id="@+id/nav_midlands_mammals_checklist"
                android:icon="@drawable/ic_dashboard"
                android:title="Midlands Mammals Checklist" />
        </group>
    </item>

</menu>

祝你好运。

前段时间我也遇到了同样的问题,最后我发现我没有写下面代码的第2行

navigationView = (NavigationView) findViewById(R.id.navigation_view);        
navigationView.setNavigationItemSelectedListener(this);

你确保你写的一样,否则你的听众将无法工作

使用此代码:

navigationView = (NavigationView) findViewById(R.id.navigationView); 
navigationView.bringToFront();

就我而言,我忘记了初始化导航菜单。

请遵循以下代码:

public void initSideMenu() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

编码愉快...

确保您已实现接口 NavigationView.OnNavigationItemSelectedListener。 第二点确保添加这些行,否则将不会调用 listnere。

navigationView=(NavigationView) findviewbyid(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);

不要使用

NavigationUI.setupWithNavController(navigationView, navController);

而不是这样做

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

添加这一行
navigationView.bringToFront(); 它对我有用