不从导航视图中突出显示选定的子项

Not highlighting selected child item from navigationview

我里面有一个 NavigationView 和渲染菜单。我的菜单有 3 组,每组包含几个子菜单项。我的问题是,当我 select 任何子菜单项并转到特定片段时。但是,当我将 NavigationView/drawer 重新打开到 select 另一个项目时,它不会显示最后 selected 突出显示的项目。

目前,它只是改变了 selected 子菜单项的颜色,但我希望它像下面的快照一样突出显示。

基础ActivityXML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_app_base"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_app_base"
        app:menu="@menu/activity_app_base_drawer"
        android:background="@color/white"
        app:itemBackground="@color/white" />

</android.support.v4.widget.DrawerLayout>

菜单项 XML (activity_app_base_drawer)

<item
    android:id="@+id/nav_android_title"
    android:title="Android" >
    <menu>
        <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_android_best_practices"
            android:icon="@drawable/best_practices_icon"
            android:checked="false"
            android:title="Best Practices" />
        <item
            android:id="@+id/nav_android_monthly_report"
            android:icon="@drawable/report_icon"
            android:checked="false"
            android:title="Monthly Report" />
        </group>
    </menu>
</item>
<item
    android:id="@+id/nav_iOS_title"
    android:title="iOS">
    <menu>
        <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_ios_best_practices"
            android:icon="@drawable/best_practices_icon"
            android:checked="false"
            android:title="Best Practices" />
        <item
            android:id="@+id/nav_ios_monthly_report"
            android:icon="@drawable/report_icon"
            android:checked="false"
            android:title="Monthly Report" />
        </group>
    </menu>
</item>
<item
    android:id="@+id/nav_communicate_title"
    android:title="Other">
    <menu>
        <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_terms"
            android:icon="@drawable/terms_icon"
            android:checked="false"
            android:title="Terms &amp; Conditions" />
        <item
            android:id="@+id/nav_about_us"
            android:icon="@drawable/about_icon"
            android:checked="false"
            android:title="About Us" />
        </group>
    </menu>
</item>

您可以使用元素中的 android:checkable 属性为单个菜单项定义可检查行为,或者使用 android:checkableBehavior 为整个组定义可检查行为element.so 中的属性为主菜单

设置了一个 checkableBehavior
<menu>
<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_android_title"
        android:title="Android" >
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_android_best_practices"
                    android:icon="@drawable/best_practices_icon"
                    android:checked="false"
                    android:title="Best Practices" />
                <item
                    android:id="@+id/nav_android_monthly_report"
                    android:icon="@drawable/report_icon"
                    android:checked="false"
                    android:title="Monthly Report" />
            </group>
        </menu>
    </item>
    <item
        android:id="@+id/nav_iOS_title"
        android:title="iOS">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_ios_best_practices"
                    android:icon="@drawable/best_practices_icon"
                    android:checked="false"
                    android:title="Best Practices" />
                <item
                    android:id="@+id/nav_ios_monthly_report"
                    android:icon="@drawable/report_icon"
                    android:checked="false"
                    android:title="Monthly Report" />
            </group>
        </menu>
    </item>
    <item
        android:id="@+id/nav_communicate_title"
        android:title="Other">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_terms"
                    android:icon="@drawable/terms_icon"
                    android:checked="false"
                    android:title="Terms &amp; Conditions" />
                <item
                    android:id="@+id/nav_about_us"
                    android:icon="@drawable/about_icon"
                    android:checked="false"
                    android:title="About Us" />
            </group>
        </menu>
    </item>
</group>
</menu>

这是完美的代码!

NavigationView

中添加三个属性

app:itemIconTint
app:itemTextColor
app:itemBackground

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_app_base"
    app:menu="@menu/activity_app_base_drawer"
    android:background="@color/white"
    app:itemIconTint="@drawable/navigation_view_icon_tint_selector"
    app:itemTextColor="@drawable/navigation_view_text_selector"
    app:itemBackground="@drawable/ripple_navigation_selector"
 />

navigation_view_icon_tint_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is used when the Navigation Item is checked -->
<item android:color="#005481" android:state_checked="true" />
<item android:color="#005481" android:state_pressed="true"></item>
<item android:color="#005481" android:state_activated="true"></item>
<!-- This is the default text color -->
<item android:color="#525252" />

navigation_view_text_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is used when the Navigation Item is checked -->
<item android:color="#005481" android:state_checked="true" />
<item android:color="#005481" android:state_pressed="true"></item>
<item android:color="#005481" android:state_activated="true"></item>
<!-- This is the default text color -->
<item android:color="#525252" />

ripple_navigation_selector

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#ffdbdb" android:state_pressed="true"/>
<item android:drawable="#ffdbdb" android:state_activated="true"/>
<item android:drawable="#ffdbdb" android:state_checked="true"/>
<item android:drawable="#FFFFFF"/>