NavigationDrawer - 选择的不同组中的项目

时间:2015-07-29 08:44:36

标签: android navigation-drawer

我的菜单包含两个组有问题。情况是,当我从group_2中选择任何项目时,group_1中的最后一个选定项目仍处于选中/突出显示状态。

任何想法如何解决?

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();
}

1 个答案:

答案 0 :(得分:3)

试试这个:

  private int selectedItem = 0; 

  private void setSelectedItem(MenuItem menuItem, int position) {

          mNavigationView.getMenu().getItem(selectedItem).setChecked(false);
          mNavigationView.getMenu().getItem(position).setChecked(true);

          if (menuItem.getGroupId() == R.id.group_2) {
                    mNavigationView.getMenu().setGroupCheckable(R.id.group_1, false, true);
                    mNavigationView.getMenu().setGroupCheckable(R.id.group_2, true, true);
                } else {
                    mNavigationView.getMenu().setGroupCheckable(R.id.group_1, true, true);
                    mNavigationView.getMenu().setGroupCheckable(R.id.group_2, false, true);
                }
                //Update highlighted item in the navigation menu
                menuItem.setChecked(true);
                selectedItem = position;
    }