Navigation Drawer menu items selected within different groups

前端 未结 5 1121
南笙
南笙 2021-02-13 11:44

I have a working Navigation Drawer and having some issues with menuItem.setChecked(true); when using groups and headers within the menu. It\'s not highlighting menu

5条回答
  •  粉色の甜心
    2021-02-13 12:16

    Try this:

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

提交回复
热议问题