I have recently started converting my android app to use the latest support library called support:design.
While implementing the new NavigationView i've stumbled upon a problem displaying the selected menu items.
My navdrawer_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/navigation_item_home"
android:icon="@drawable/ic_home_black"
android:title="@string/navdrawer_item_home" />
</group>
<item
android:id="@+id/navigation_subheader"
android:title="@string/navdrawer_subheader_title1">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/navigation_sub_item1"
android:icon="@drawable/ic_home_black"
android:title="@string/navdrawer_sub_item1" />
</group>
</menu>
</item>
</menu>
Next I set the menu's item to checked in my onNavigationItemSelected:
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
displayView(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
return true;
}
This works great if I only use normal menu items between the tags but it does not work very well for subheaders. Clicking on sub items wont set them checked untill i've clicked the same item twice and it won't uncheck the any item that checked previously.
It ends up looking like this:
I resolved this recurrent problem in this way that works very well.
We must simply memorize the id of the selected item, recharge your menu on your NavigationView and select the item again.
For that you need to have your menu drawer like way :
So, my solution :