简体   繁体   English

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

[英]Uncheck MenuItem in Navigation Drawer after return to main activity

I have a small problem with navigation drawer. 我的导航抽屉有个小问题。 I want to have nothing "checked" after return to main activity, here is more about my problem: 返回主要活动后,我什么都不想“检查”,这是我的更多问题:

When I am starting an activity, I have menu like that: 当我开始一项活动时,我会看到如下菜单: 在此处输入图片说明

That is good :) When I click "User profil" or other menu item I am opening new activity. 很好:)当我单击“用户配置文件”或其他菜单项时,我正在打开新活动。 I don't have any menu in this "new activity", only return button, so I am returning to main activity. 我在“新活动”中没有任何菜单,仅返回按钮,因此我将返回主活动。 And here is a problem, when I return to main activity and I open navigation drawer, it is looking like: 这是一个问题,当我返回主活动并打开导航抽屉时,它看起来像:

在此处输入图片说明

What I have to do to uncheck this menu item that I previous open? 要取消选中我先前打开的菜单项,该怎么做?

//EDIT Code of Navigation Drawer: //导航抽屉的编辑代码:

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

I'd uncheck your menuItems in the "onPause" or "onResume" method. 我会取消选中“ onPause”或“ onResume”方法中的menuItems。 I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. 我假设您对menuItem.isChecked的调用返回false,因为在调用侦听器之后android会将您的menuItem设置为选中状态。 The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View? arsent的答案显示了如何取消选中菜单项: 如何取消取消导航视图中的选中项? It is also possible to uncheck menu items in onDrawerClosed listner: This code works: 也可以取消选中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;
                    }

                }

Firstly, Let's create an int variable to get the size of your navigation menu. 首先,让我们创建一个int变量来获取导航菜单的大小。

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

Second, make a for loop to un-check all the menu items in your navigation view 其次,进行for循环以取消选中导航视图中的所有菜单项

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

Third, let's put our two steps of code block into onNavigationItemSelected block. 第三,让我们将代码块的两个步骤放入onNavigationItemSelected块中。 Your code will be looks like this. 您的代码将如下所示。

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

Finally, let's put menuItem.setChecked(true); 最后,让我们放置menuItem.setChecked(true); and drawerLayout.closeDrawers(); drawerLayout.closeDrawers(); in every case of your switch fun. 在您切换乐趣的每种情况下。 And then do all the work you did in onDrawerClosed in onNavigationItemSelected . 然后完成在onDrawerClosed中的onNavigationItemSelected所做的所有工作。 Delete all the code in onDrawerClosed . 删除onDrawerClosed所有代码。 As for me, I love to work all the code in single block. 对于我来说,我喜欢将所有代码都放在一个块中。 We can create another fun for reducing code lines. 我们可以为减少代码行数创造另一个乐趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM