返回主活动后,取消选中导航抽屉中的MenuItem。

时间:2018-11-14 23:06:08

标签: android xml

导航抽屉有一个小问题。返回主要活动后,我什么都不想“检查”,以下是有关我的问题的更多信息:

当我开始一项活动时,我会看到如下菜单: enter image description here

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

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

2 个答案:

答案 0 :(得分:1)

我会取消选中“ onPause”或“ onResume”方法中的menuItems。我假设您对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;
                    }

                }

答案 1 :(得分:1)

首先,让我们创建一个int变量来获取导航菜单的大小。

int size = navigationView.getMenu().size();

第二,进行for循环以取消选中导航视图中的所有菜单项

for (int i = 0; i < size; i++)
        navigationView.getMenu().getItem(i).setChecked(false);

第三,我们将代码块的两个步骤放入onNavigationItemSelected块中。您的代码将如下所示。

navigationView.setNavigationItemSelectedListener(
        new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                // close drawer when item is tapped
                int size = navigationView.getMenu().size();
                for (int i = 0; i < size; i++)
                navigationView.getMenu().getItem(i).setChecked(false);
                //Your switch code here.
                }

最后,我们将menuItem.setChecked(true);drawerLayout.closeDrawers();放在每次切换乐趣中。然后完成您在onDrawerClosed的{​​{1}}中所做的所有工作。删除onNavigationItemSelected中的所有代码。对于我来说,我喜欢将所有代码都放在一个块中。我们可以为减少代码行创造另一个乐趣。