How to keep single checkableBehavior mode in drawe

2019-04-18 05:58发布

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>

2条回答
Anthone
2楼-- · 2019-04-18 06:51

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.

查看更多
Fickle 薄情
3楼-- · 2019-04-18 07:01

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

查看更多
登录 后发表回答