How to uncheck checked items in Navigation View?

前端 未结 12 1397
夕颜
夕颜 2020-12-15 15:48

I know it\'s possible to highlight a navigation view item by calling setCheckedItem() or return true value in onNavigationItemSelected to display t

相关标签:
12条回答
  • 2020-12-15 16:42

    Joao's solutions didn't not work for me as totally expected. This would lead to a blank space from unchecked Item View on my Navigation.

    Just make sure to set the view as gone:

    <item
        android:id="@+id/your_menu_item_id_to_hide"
        android:title=""
        android:visible="false"/>
    
    
    bottomNavigationView.getMenu().findItem(R.id.your_menu_item_id_to_hide).setChecked(true);
        bottomNavigationView.findViewById(R.id.your_menu_item_id_to_hide).setVisibility(View.GONE);
    

    Arsent solution is not necessary in this case.

    0 讨论(0)
  • 2020-12-15 16:47

    To uncheck all MenuItems including SubMenu items you have to use recursion -

        private void unCheckAllMenuItems(@NonNull final Menu menu) {
            int size = menu.size();
            for (int i = 0; i < size; i++) {
                final MenuItem item = menu.getItem(i);
                if(item.hasSubMenu()) {
                    // Un check sub menu items
                    unCheckAllMenuItems(item.getSubMenu());
                } else {
                    item.setChecked(false);
                }
            }
        }
    

    Call above method for unchecking all items, like below -

    unCheckAllMenuItems(navigationView.getMenu());
    
    0 讨论(0)
  • 2020-12-15 16:48

    I saw @arsent solution and gave it a try, and it will indeed do what you want, which is to unselect all the items... but, I was having an issue in the following scenario:

    1. Select menu item 1 (using NavigationView#setCheckedItem)
    2. Unselect all the items as per @arsent's solution
    3. Select menu item 1 again (using NavigationView#setCheckedItem)

    In this scenario, item 1 will not be marked as checked. That's because internally the navigation view keeps track of the previously selected item set in step 1, which doesn't change in step 2, and it just skips step 3 because the previously selected item is the same as the one we're selecting now.

    My suggestion (and an alternative solution) to avoid this is just having a dummy invisible item and use NavigationView#setCheckedItem to select that item whenever you want to unselect all, like so

    <item
        android:id="@+id/menu_none"
        android:title=""
        android:visible="false"/>
    

    To unselect all just do

    mNavigationView.setCheckedItem(R.id.menu_none);
    
    0 讨论(0)
  • 2020-12-15 16:52

    just make your items non checkable like so

    <item
        android:id="@+id/profile_item"
        android:checkable="false"
        android:title="@string/profile"/>
    
    0 讨论(0)
  • 2020-12-15 16:52

    To uncheck it inside NavigationItemSelectedListener I had to use post (to UI thread):

    App.getHandler().post(() -> menuItem.setChecked(false));
    

    Full example:

     NavigationView navigationView = findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(
                    menuItem -> {
                        menuItem.setChecked(true);
    
                        mDrawerLayout.closeDrawers();
    
                        switch (menuItem.getItemId()) {
                            ...
                        }
    
                        App.getHandler().post(() -> menuItem.setChecked(false));
    
                        return true;
                    });
    

    p.s. in my case App.getHandler() returns Handler instance for UI Thread Lopper

    0 讨论(0)
  • 2020-12-15 16:53

    This will uncheck the items:

        int size = mNavigationView.getMenu().size();
        for (int i = 0; i < size; i++) {
            mNavigationView.getMenu().getItem(i).setChecked(false);
        }
    
    0 讨论(0)
提交回复
热议问题