gpt4 book ai didi

android - NavigationDrawer - 来自不同组的项目被选中

转载 作者:搜寻专家 更新时间:2023-11-01 08:42:02 27 4
gpt4 key购买 nike

我对包含两组的菜单有疑问。这种情况是当我从 group_2 中选择任何项目时,从 group_1 中最后选择的项目保持选中/突出显示。

有什么解决办法吗?

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

最佳答案

试试这个:

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

关于android - NavigationDrawer - 来自不同组的项目被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31695506/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com