Independent checkableBehavior = "single" groups in NavigationView / DrawerLayout

I have the following activity_nav_drawer_drawer.xml as an app menu: for NavigationView in 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>

Activity implements NavigationView.OnNavigationItemSelectedListenerwith the current listener.

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    return true;
}

The problem is that if one item is selected from one group, it does not select the selected item in another group. I need each group to have an independent selection. How do you do this?

+4
source share
1 answer

NavigationView, (, this one this) Google bugtracker .

enter image description here

, :

  • MenuItem
  • - .
  • android:checkableBehavior xml android:checkable="true" MenuItem

:

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;
}

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>

,

+4

Source: https://habr.com/ru/post/1625494/


All Articles