NavigationView has erratic behavior with selection

2019-07-24 09:29发布

问题:

OK, the behavior or my NavigationView works. I click on the settings and it takes me there. I click on my first item, it does what it should. I click on the second item, it does what it should.

However, my issue is with the selection state. I will click on an item, and it will not select it. The only selection that works is my Settings menu. When I click on that item, the settings menu will become selected, along with all previous items I clicked on.

Scenarios

  1. On App Load

    • Go to NavigationView (A, B, Settings)
    • Click on A Item
    • App navigates to the right place (nothing is selected)
    • Go to NavigationView
    • Click on Settings
    • App navigates to Settings Activity (Settings menu and A menu are selected)
  2. On App Load

    • Go to NavigationView (A, B, Settings)
    • Click on A Item
    • App navigates to the right place (nothing is selected)
    • Go to NavigationView
    • Click on B Item
    • App navigates to the right place (nothing is selected)
    • Go to NavigationView
    • Click on Settings
    • App navigates to Settings Activity (Settings menu and A, B menus are selected)
  3. On App Load

    • Go to NavigationView (A, B, Settings)
    • Click on Settings
    • App navigates to Settings Activity (Settings menu is selected)
  4. On App Load

    • Go to NavigationView (A, B, Settings)
    • Click on Settings
    • App navigates to Settings Activity (Settings menu is selected)
    • Go to NavigationView
    • Click on B Item
    • App navigates to the right place (Settings is selected)
    • Go to NavigationView
    • Click on Settings
    • App navigates to Settings Activity (Settings menu and B menu are selected)
  5. On App Load

    • Go to NavigationView (A, B, Settings)
    • Click on A Item
    • App navigates to the right place (nothing is selected)
    • Go to NavigationView
    • Click on A Item
    • App navigates to the right place (A is selected)

Code

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">


    <item
        android:id="@+id/movie_heading"
        android:title="Movies"
        android:enabled="false">

        <menu
            android:id="@+id/navigationGroupMovies"
            android:checkableBehavior="single">

            <item
                android:id="@+id/navigationOnline"
                android:icon="@drawable/ic_movie_creation_white_24dp"
                android:title="Find Online"
                android:checkable="true"/>

            <item
                android:id="@+id/navigationMyCollection"
                android:icon="@drawable/abc_btn_rating_star_on_mtrl_alpha"
                android:title="My Collections"
                android:checkable="true"/>

        </menu>

    </item>

    <group
        android:id="@+id/navigationGroupSetting"
        android:checkableBehavior="single">

        <item
            android:id="@+id/navigationSetting"
            android:icon="@drawable/ic_settings_white_24dp"
            android:title="Settings" />

    </group>

</menu>

state_navigation_text.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
          android:color="@color/accent"  />

    <item android:state_focused="true"
          android:color="@color/primary"/>

    <item android:color="@color/primary_dark" />

</selector>

When Navigation Item is Clicked

@Override
public boolean onNavigationItemSelected(MenuItem item) {

    item.setChecked(true);

    switch (item.getItemId()) {

        case R.id.navigationSetting:

            startActivity(new Intent(this, SettingsActivity.class));
            break;

        case R.id.navigationOnline:         setCurrentTabs(API.WEB_GROUP); break;
        case R.id.navigationMyCollection:   setCurrentTabs(API.OFFLINE_GROUP); break;

        default: // Do nothing

    }

    closeDrawer();

    return true;

}