NavigationDrawer – items from different groups selected

I have a problem with menu that contains two groups. The case is when I select any item from “group_2”, last selected item from “group_2” remains selected/highlighted. Any ideas how to fix that?

drawer_view.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single" android:id="@+id/group_1">
        <item
            android:id="@+id/leagues_in_progress_item"
            android:icon="@drawable/ic_format_line_spacing_black_48dp"
            android:title="@string/title_activity_leagues_in_progress" />
        <item
            android:id="@+id/last_matches_item"
            android:icon="@drawable/ic_access_alarm_black_48dp"
            android:title="@string/title_activity_last_matches" />

    </group>
    <group android:checkableBehavior="single" android:id="@+id/group_2">
        <item
            android:id="@+id/archive_item"
            android:icon="@drawable/ic_folder_open_black_48dp"
            android:title="@string/title_activity_archive" />
        <item
            android:id="@+id/put_score_item"
            android:icon="@drawable/ic_add_circle_outline_black_48dp"
            android:title="@string/title_activity_put_score" />
    </group>
</menu> 

MyActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // ...

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    this.mDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout, R.string.app_name, R.string.app_name);
    this.mDrawerLayout.setDrawerListener(this.mDrawerToggle);

    NavigationView navigationView = (NavigationView)findViewById(R.id.left_drawer);

    this.addLeaguesSubmenu(navigationView);
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    displayMenuFragment(menuItem);
                    return true;
                }
            });

    if (savedInstanceState == null) {
        this.displayMenuFragment(navigationView.getMenu().getItem(0));
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // toggle nav drawer on selecting action bar app icon/title
    if (this.mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
public void displayMenuFragment(MenuItem menuItem)
{
    // ...

    menuItem.setChecked(true);
    this.mDrawerLayout.closeDrawers();
}


Source: android

Leave a Reply