I have the following activity_nav_drawer_drawer.xml as the app:menu for a NavigationView in a DrawerLayout.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/nav_group_features"
android:checkableBehavior="single">
<item android:id="@+id/nav_feature1"
android:icon="@drawable/ic_build_black"
android:title="Feature 1"/>
<item android:id="@+id/nav_feature2"
android:icon="@drawable/ic_account_balance_black"
android:title="Feature 2"/>
<item android:id="@+id/nav_feature3"
android:icon="@drawable/ic_folder_black"
android:title="Feature 3"/>
</group>
<item android:title="Select Project">
<group android:id="@+id/nav_group_projects"
android:checkableBehavior="single">
<item android:id="@+id/nav_project1"
android:icon="@drawable/ic_domain_black"
android:title="Project 1"/>
<item android:id="@+id/nav_project2"
android:icon="@drawable/ic_domain_black"
android:title="Project 2"/>
<item android:id="@+id/nav_project3"
android:icon="@drawable/ic_domain_black"
android:title="Project 3"/>
</group>
</item>
</menu>
Here is the containing DrawerLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/app_bar_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_nav_drawer"
app:menu="@menu/activity_nav_drawer_drawer"/>
</android.support.v4.widget.DrawerLayout>
The activity implements NavigationView.OnNavigationItemSelectedListener
with the flowing listener.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return true;
}
The problem is if one item is selected from one group, it un-selects the selected item in the other group. I need each groups to have an independent selection. How do you do this?
For me, it looks like a bug in NavigationView
itself and there're several similar issues (like this one or this) in Google's bugtracker regarding it.
However, you can archive it, by doing this:
MenuItem
s from each groupandroid:checkableBehavior
from the xml and add android:checkable="true"
for each MenuItem
There's actual implementation:
MenuItem selectedFeature, selectedProject;
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
if (item.getGroupId() == R.id.group_feature) {
if (selectedFeature != null) {
selectedFeature.setChecked(false);
}
selectedFeature = item;
selectedFeature.setChecked(true);
} else if (item.getGroupId() == R.id.group_projects) {
if (selectedProject != null) {
selectedProject.setChecked(false);
}
selectedProject = item;
selectedProject.setChecked(true);
}
return false;
}
And activity_main_drawer.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/group_feature">
<item android:id="@+id/nav_feature1"
android:icon="@drawable/ic_menu_camera"
android:checkable="true"
android:title="Feature 1"/>
<item android:id="@+id/nav_feature2"
android:icon="@drawable/ic_menu_gallery"
android:checkable="true"
android:title="Feature 2"/>
<item android:id="@+id/nav_feature3"
android:icon="@drawable/ic_menu_slideshow"
android:checkable="true"
android:title="Feature 3"/>
</group>
<item android:title="Select Project">
<menu>
<group
android:id="@+id/group_projects">
<item android:id="@+id/nav_project1"
android:icon="@drawable/ic_menu_manage"
android:checkable="true"
android:title="Project 1"/>
<item android:id="@+id/nav_project2"
android:icon="@drawable/ic_menu_share"
android:checkable="true"
android:title="Project 2"/>
<item android:id="@+id/nav_project3"
android:icon="@drawable/ic_menu_send"
android:checkable="true"
android:title="Project 3"/>
</group>
</menu>
</item>
</menu>
I hope, it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With