I have this 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>
But when selecting the nav_settings,
the item is not check.
The item should be check just like the other items above.
This is my code on setting the item checked.
The fragment will just work fine.
but the selector on the nav_settings didn't work..
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();
}
I don't think nesting items like your code is allowed.
If you do like below, it may work:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.