我的导航视图中有 3 个菜单,每个菜单都有更多的项目。当我按下菜单中的每个项目时遇到问题,我希望选中选中的项目但未选中其他项目。下面是图片,以便您可以看到问题所在:
使用 item.setChecked(true) 进行选择;在 onNavigationItemSelected(MenuItem 菜单)中。我想要的只是选择一项,所以当我检查一项时,我希望不选择其他项目。
我在谷歌上搜索,但通常他们说我需要将每个菜单分成组 android:checkableBehavior="single" 我所做的,但它仍然不起作用:
编辑开始:
我只是找到解决方案,而不是item.setChecked(true)我写的是navigationView.setCheckedItem(R.id.itemId); 现在一切正常。
编辑结束
这是drawer_menu.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Animals">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/dog"
android:icon="@drawable/ic_action_dog"
android:title="Domestic Animals" />
<item
android:id="@+id/wild"
android:icon="@drawable/ic_action_dog"
android:title="Wild Animals" />
<item
android:id="@+id/bird"
android:icon="@drawable/ic_action_bird"
android:title="Birds" />
<item
android:id="@+id/fish"
android:icon="@drawable/ic_action_fish"
android:title="Sea Animals" />
<item
android:id="@+id/insects"
android:icon="@drawable/ic_action_bubamara"
android:title="Insects" />
</group>
</menu>
</item>
<item android:title="Others">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/car"
android:icon="@drawable/ic_action_car"
android:title="Cars" />
<item
android:id="@+id/smiley"
android:icon="@drawable/ic_action_smiley"
android:title="Laugh" />
<item
android:id="@+id/earth"
android:icon="@drawable/ic_action_earth"
android:title="Nature" />
<item
android:id="@+id/sounds"
android:icon="@drawable/ic_action_zvucnik"
android:title="Effects" />
<item
android:id="@+id/tools"
android:icon="@drawable/ic_action_clock"
android:title="Others" />
</group>
</menu>
</item>
<item android:title="Change pictures">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/change_picture_id"
android:icon="@drawable/ic_action_promjena_slike"
android:title="Change Picture"></item>
</group>
</menu>
</item>
</menu>
这是主活动中导航抽屉菜单项单击事件的代码:
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.dog:
homeFragment.sortiranje("Domestic", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.wild:
homeFragment.sortiranje("Wild", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.bird:
homeFragment.sortiranje("Birds", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.fish:
homeFragment.sortiranje("Sea", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.insects:
homeFragment.sortiranje("Insects", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.car:
homeFragment.sortiranje("Cars", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.smiley:
homeFragment.sortiranje("Laugh", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.earth:
homeFragment.sortiranje("Nature", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.sounds:
homeFragment.sortiranje("Effects", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.tools:
homeFragment.sortiranje("Others", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.change_picture_id:
item.setChecked(true);
if (tagonja == 0) {
tagonja = 1;
homeFragment.promjenaSlike(tagonja);
} else if (tagonja == 1) {
tagonja = 0;
homeFragment.promjenaSlike(tagonja);
}
drawerLayout.closeDrawers();
break;
}
return false;
}
});