我在DrawerLayout中有以下activity_nav_drawer_drawer.xml作为app:menu的菜单。
<?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>
这是包含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>
该活动使用流动的侦听器实现NavigationView.OnNavigationItemSelectedListener
。
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return true;
}
问题是如果从一个组中选择了一个项目,它将取消选择另一个组中的所选项目。我需要每个小组都有一个独立的选择。你是怎么做到的?
答案 0 :(得分:4)
对我来说,它看起来像是NavigationView
本身的一个错误,Google的错误跟踪器中有几个类似的问题(例如this一个或this)。
但是,您可以将其归档:
MenuItem
android:checkableBehavior
并为每个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>
我希望,这有帮助