gpt4 book ai didi

android - 返回主 Activity 后取消选中抽屉导航中的 MenuItem

转载 作者:行者123 更新时间:2023-11-29 18:39:40 25 4
gpt4 key购买 nike

抽屉导航有一个小问题。返回主要 Activity 后,我不想“检查”任何内容,这里是关于我的问题的更多信息:

当我开始一项 Activity 时,我有这样的菜单: enter image description here

很好 :) 当我单击“用户配置文件”或其他菜单项时,我正在打开新 Activity 。我在这个“新 Activity ”中没有任何菜单,只有返回按钮,所以我要返回主要 Activity 。这是一个问题,当我返回主 Activity 并打开抽屉导航时,它看起来像:

enter image description here

要取消选中我之前打开的这个菜单项,我需要做什么?

//编辑抽屉导航代码:

mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);

ActionBar actionbar = this.getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);

mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_user:
//Save info about clicked menu item to open correct activity when "onDrawerClosed" listener will be call
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_USER_PROFIL;
return true;
case R.id.action_settings:
choseIntentFromDrawerLayout=EventContract.EventEntry.MENU_SETTINGS;
return true;
case R.id.action_about:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_ABOUT;
return true;
case R.id.action_log_out:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_LOG_OUT;
return true;
default:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}

//UNCHECK? - NO WORKING
if(menuItem.isChecked()){
menuItem.setChecked(false);
}
return true;

}
});

mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}

@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}

@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}

}

@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);

最佳答案

我会取消选中“onPause”或“onResume”方法中的菜单项。我假设您对 menuItem.isChecked 的调用返回 false,因为 android 会在调用监听器后将您的 menuItem 设置为已选中。 arsent 的回答显示了如何取消选中菜单项:How to uncheck checked items in Navigation View?也可以取消选中 onDrawerClosed 监听器中的菜单项:此代码有效:

@Override
public void onDrawerClosed(View drawerView) {
//Solution:
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++) {
navigationView.getMenu().getItem(i).setChecked(false);
}
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}

}

关于android - 返回主 Activity 后取消选中抽屉导航中的 MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53310076/

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