onNavigationItemSelected在NavigationView中不起作用 [英] onNavigationItemSelected not working in NavigationView

查看:163
本文介绍了onNavigationItemSelected在NavigationView中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮助我处理导航抽屉中的片段,由于某些原因,我无法使它们正常工作,并且所有代码看起来都正确.

Please can someone help me with fragments from the navigation drawer, for some reason I can't get them to work and all the code looks right.

此处是源代码的链接.

推荐答案

看看您的 MainActivity.java .

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

You have implemented the callbacks for NavigationView.OnNavigationItemSelectedListener in MainActivity as below,

@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 .

In this method you are creating a local OnNavigationItemSelectedListener.

因此,您没有使用在 MainActivity 中覆盖的 OnNavigationItemSelectedListener .

So you are not using the OnNavigationItemSelectedListener that you have overridden in MainActivity.

解决方案是将 this 用作 setNavigationItemSelectedListener 的参数.这样,您所有的点击将转到 MainActivity onNavigationItemSelected ,而不是转到本地的 onNavigationItemSelected .

The solution is to use this as argument for setNavigationItemSelectedListener. By doing this all your clicks will go the onNavigationItemSelected of MainActivity rather than going to the local onNavigationItemSelected.

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

还要将本地 onNavigationItemSelected 中的代码移动到 MainActivity onNavigationItemSelected 中.

Also move the code in the local onNavigationItemSelected to the onNavigationItemSelected of MainActivity.

所以您的 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 ,以解决导航抽屉中的多选问题

Also change your activity_main_drawer_view.xml as follows to solve the multiple selection issue you have in the Navigation Drawer,

<?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>

祝你好运.

这篇关于onNavigationItemSelected在NavigationView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆