当我点击一个不同组(特殊)的项目时,在NavigationDrawer中保留所选项目时遇到问题。
...
<item
android:id="@+id/item_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/drawer_notification"
android:visible="true" />
</group>
<group android:id="@+id/special">
<item
android:id="@+id/item_settings"
android:icon="@drawable/ic_settings_48dp"
android:title="@string/drawer_settings" />
<item
android:id="@+id/item_feedback"
android:icon="@drawable/ic_chat_bubble_black_24dp"
android:title="@string/drawer_feedback" />
<item
android:id="@+id/item_logout"
android:icon="@drawable/ic_arrow_back_black_24dp"
android:title="@string/drawer_logout" />
</group>
这是我的MainActivity中的切换案例:
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.item_notifications:
mCurrentSelectedPosition = 6;
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, BaseFragment.newInstance("Notifications"))
.commit();
mDrawerLayout.closeDrawers();
return true;
case R.id.item_settings:
mCurrentSelectedPosition = 7;
mDrawerLayout.closeDrawers();
Snackbar.make(findViewById(R.id.container), "Settings", Snackbar.LENGTH_SHORT).show();
return true;
case R.id.item_feedback:
mCurrentSelectedPosition = 8;
mDrawerLayout.closeDrawers();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, FeedbackFragment.getInstance())
.commit();
return true;
case R.id.item_logout:
mDrawerLayout.closeDrawers();
session.logoutUser(MainActivity.this);
return true;
}
}
我的代码没有任何问题。只是当我点击抽屉中的另一个组时,navigationDrawer不会保留我选择的项目。
答案 0 :(得分:1)
您需要在菜单组中添加以下行:
机器人:checkableBehavior =&#34;单&#34;
将帮助您保留所选状态。
<group android:id="@+id/special"
android:checkableBehavior="single">
<item
android:id="@+id/item_settings"
android:icon="@drawable/ic_settings_48dp"
android:title="@string/drawer_settings" />
<item
android:id="@+id/item_feedback"
android:icon="@drawable/ic_chat_bubble_black_24dp"
android:title="@string/drawer_feedback" />
<item
android:id="@+id/item_logout"
android:icon="@drawable/ic_arrow_back_black_24dp"
android:title="@string/drawer_logout" />
</group>
查看此official blog post以获取更多参考资料......
希望它会有所帮助。