How to add a collapsible menu item inside navigation drawer in android?

后端 未结 3 1543
南笙
南笙 2021-02-07 17:56

I have a DrawerLayout enclosing a NavigationView and this layout activity serves as a common Navigation drawer for all the activities in my app. I am p

3条回答
  •  一生所求
    2021-02-07 18:41

    In menu layout file:

    
    
        
        
        
    
    

    In MyActivity.java - load desired menu:

    public void LoadNavMenu(int iMenu){
        navigationView.getMenu().clear();
        navigationView.inflateMenu(iMenu);
        navigationView.getMenu().setGroupVisible(R.id.HelpGroup,false);
        navigationView.getMenu().setGroupVisible(R.id.SetupGroup,false);
    }
    

    And OnNavigationItemSelected:

    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        navigationView.getMenu().setGroupVisible(R.id.HelpGroup,false);
        navigationView.getMenu().setGroupVisible(R.id.SetupGroup,false);
        switch (item.getItemId()){
            case R.id.HelpGr:
                navigationView.getMenu().setGroupVisible(R.id.HelpGroup,true);
                navigationView.getMenu().setGroupVisible(R.id.SetupGroup,false);
                return true;
            case R.id.SetupGr:
                navigationView.getMenu().setGroupVisible(R.id.SetupGroup,true);
                navigationView.getMenu().setGroupVisible(R.id.HelpGroup,false);
                return true;
        }    
        base.closeDrawer(GravityCompat.START);
        return true;
    }
    

    I have 1 item - Setup - and when user select it - I set Group with id - SetupGroup - visible and return without closing drawer. Else - I set all groups invisible.

提交回复
热议问题