NavigationView所选项目

时间:2015-11-28 03:41:26

标签: android android-studio android-navigationview

我有这个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:checked="true"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="Home" />
        <item
            android:id="@+id/nav_search"
            android:icon="@drawable/ic_search_black"
            android:title="Search Location" />
        <item
            android:id="@+id/nav_fav"
            android:icon="@drawable/ic_favorite"
            android:title="Favorites" />
        <item
            android:id="@+id/nav_recent"
            android:icon="@drawable/ic_nav_route"
            android:title="Recent Location" />
        <item
            android:id="@+id/nav_route"
            android:icon="@drawable/ic_place"
            android:title="Route" />
    </group>

    <item android:title="Others">
        <menu>
            <item
                android:id="@+id/nav_settings"
                android:icon="@drawable/ic_settings"
                android:title="Settings" />
            <item
                android:id="@+id/nav_about"
                android:icon="@android:drawable/ic_menu_send"
                android:title="About" />
        </menu>
    </item>

</menu>

但是当选择nav_settings时,
该项目未检查。
该项目应该像上面的其他项目一样进行检查 这是我设置项目检查的代码 片段将正常工作。
但是nav_settings上的选择器没有工作..

 public void selectDrawerItem(MenuItem menuItem) {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            Fragment fragment = null;

            Class fragmentClass;
            switch(menuItem.getItemId()) {
                case R.id.nav_home:
                    fragmentClass = FragmentMap.class;
                    setTitle("Map");
                    break;

                case R.id.nav_search:
                    fragmentClass = FragmentSearchLoc.class;
                    Global.setCurrentItem =2;
                    break;
                case R.id.nav_recent:
                    fragmentClass = FragmentSearchLoc.class;
                    Global.setCurrentItem =0;
                    break;
                case R.id.nav_fav:
                    fragmentClass = FragmentSearchLoc.class;
                    Global.setCurrentItem =1;
                    break;
                case R.id.nav_route:
                    fragmentClass = FragmentRoute.class;
                    setTitle("Routing");
                    break;
                case R.id.nav_settings:
                    fragmentClass = FragmentSettings.class;
                    setTitle("Settings");
                    break;
                default:
                    fragmentClass = FragmentMap.class;
            }

            try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }


            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

            drawer
            menuItem.setChecked(true);
            drawer.closeDrawers();
        }

1 个答案:

答案 0 :(得分:0)

我认为不允许像代码那样嵌套项目。

如果您喜欢以下内容,可能会有效:

<group android:checkableBehavior="single">
     <item
         android:id="@+id/nav_settings"
         android:icon="@drawable/ic_settings"
         android:title="Settings" />
     <item
         android:id="@+id/nav_about"
         android:icon="@android:drawable/ic_menu_send"
         android:title="About" />
</group>