在NavigationView如何设置共享的所有组可检查的行为?如何设置、行为、NavigationView

2023-09-04 23:08:33 作者:还有明天怕什么

我创建了两个组,唯一的ID(我需要一分),他们都具有 checkableBehavior 设置为单的。这使得来自不同群体的多个项目同时进行检查,而这正是我想避免的。我想有一个项目检查开到最大,在所有群体。

I've created two groups with unique ids (I need a divider) and they both have checkableBehavior set to single. This allows multiple items from different groups to be checked at once, and that's exactly what I'm trying to avoid. I'd like to have one item checked at maximum, across all groups.

由于我没有找到任何办法做到这一点在XML中,我试图实施 onNavigationItemSelected 一个简单的逻辑,取消了previous菜单项:

Since I haven't found any way to do this in XML, I tried to implement a simple logic in onNavigationItemSelected to uncheck the previous menu item:

if (previousItem != null)
   previousItem.setChecked(false);
currentItem.setChecked(true);

setChecked(假)方法从来没有为我工作 - 该项目保持检查

but setChecked(false) method has never worked for me - the item stays checked.

下面是我的示例code:

Here's my sample code:

menu_navigation.xml:

menu_navigation.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
        android:id="@+id/nav_group_1"
        android:checkableBehavior="single">
    <item
            android:id="@+id/nav_feed"
            android:title="@string/feed"/>
    <item
            android:id="@+id/nav_people"
            android:title="@string/people"/>
</group>
<group
        android:id="@+id/nav_group_2"
        android:checkableBehavior="single">
    <item
            android:id="@+id/nav_settings"
            android:title="@string/settings"/>
    <item
            android:id="@+id/nav_help_feedback"
            android:title="@string/help_feedback"/>
    <item
            android:id="@+id/nav_logout"
            android:title="@string/logout"/>
</group>

NavigationItemSelectedListener:

NavigationItemSelectedListener:

 mUiNavigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        if (previousItem != null)
                           previousItem.setChecked(false);
                        currentItem.setChecked(true);
                        //...
                        changeCurrentFragment(...);
                        return true;
                    }
                });

我需要一个暗示!谢谢你。

I need a hint! Thanks.

推荐答案

下面是解决方案。

步骤1:删除

android:checkableBehavior="single"

这两个群体。

第2步:添加下面的逻辑来监听器:

Step 2: Add the following logic to the listener:

mUiNavigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setCheckable(true);
                    menuItem.setChecked(true);
                    if (mPreviousMenuItem != null) {
                        mPreviousMenuItem.setChecked(false);
                    }
                    mPreviousMenuItem = menuItem;
                    //...
                    changeCurrentFragment(...);
                    return true;
                }
            });

请注意:不是调用 menuItem.setCheckable(真)你可以设置机器人:可检查=真在XML中的每个项目。

Note: instead of calling menuItem.setCheckable(true) you can set android:checkable="true" for each item in XML.

@ Moinkhan的解决方案,应该在新的位置被选中,每次工作以及(感谢,upvoted),但我不想遍历菜单的项目。

@Moinkhan's solution should work as well (thanks, upvoted), but I didn't want to loop through menu's items each time a new position is selected.