How to keep single checkableBehavior mode in drawer menu for NavigationView when we add section?

邮差的信 提交于 2019-12-10 00:45:08

问题


I try to implement a drawer with new component of material design : NavigationView.

It's work very well. When I select an item changes its color change well with android:checkableBehavior="single".

<group
    android:checkableBehavior="single">

    <item
        android:id="@+id/drawer_home"
        android:checked="true"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/home"/>

    <item
        android:id="@+id/drawer_favourite"
        android:icon="@drawable/ic_favorite_black_24dp"
        android:title="@string/favourite"/>
    ...

    <item
        android:id="@+id/drawer_settings"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="@string/settings"/>

</group>

The problem come when I try to use section in drawer. It's this case, I can't use android:checkableBehavior="single" and I lost the color change in the selection of an item.

<item
    android:id="@+id/section"
    android:title="@string/section_title">

    <menu>
        <item
            android:id="@+id/drawer_favourite"
            android:icon="@drawable/ic_favorite_black_24dp"
            android:title="@string/favourite"/>

        <item
            android:id="@+id/drawer_downloaded"
            android:icon="@drawable/ic_file_download_black_24dp"
            android:title="@string/downloaded"/>
    </menu>

</item>

回答1:


try this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">

        <item
            android:id="@+id/drawer_home"
            android:checked="true"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/home"/>

       <item
            android:id="@+id/drawer_favourite"
            android:icon="@drawable/ic_favorite_black_24dp"
            android:title="@string/favourite"/>
       ...

       <item
            android:id="@+id/drawer_settings"
            android:icon="@drawable/ic_settings_black_24dp"
            android:title="@string/settings"/>
       <item
            android:id="@+id/section"
            android:title="@string/section_title">

            <menu>
                <group android:checkableBehavior="single">
                    <item
                        android:id="@+id/drawer_favourite"
                        android:icon="@drawable/ic_favorite_black_24dp"
                        android:title="@string/favourite"/>

                    <item
                        android:id="@+id/drawer_downloaded"
                        android:icon="@drawable/ic_file_download_black_24dp"
                        android:title="@string/downloaded"/>
                </group>
            </menu>

       </item>

    </group>
</menu>

you can check this solution for details.. I am unable to set a submenu item as checked




回答2:


As a workaround until the bug reported by Fondesa is fixed you can use this:

Menu Definition

<item android:checkable="true" ...>

Styling

<item android:state_selected="true" android:color="@color/error_color"/>

This will properly highly the menu item when selected.

Note that this will not address the requirement:

android:checkableBehavior="single"

You will have to handle that manually.



来源:https://stackoverflow.com/questions/31045008/how-to-keep-single-checkablebehavior-mode-in-drawer-menu-for-navigationview-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!