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

后端 未结 3 1544
南笙
南笙 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 18:27

    there is a trick to do this without using ExpandableListView.Add those menu items normally in menu layout file and hide/show them on item click of menu item under which you want to show them like this:

     public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
    
            NavigationView nv= (NavigationView) findViewById(R.id.nav_view);
            Menu m=nv.getMenu();
            int id = item.getItemId();
            if (id == R.id.nav_posts) {
                boolean b=!m.findItem(R.id.nav_class).isVisible();
                //setting submenus visible state
                m.findItem(R.id.nav_class).setVisible(b);
                m.findItem(R.id.nav_dept).setVisible(b);
                m.findItem(R.id.nav_batch).setVisible(b);
                m.findItem(R.id.nav_campus).setVisible(b);
                return true;
            } else if (id == R.id.nav_walls) {
                boolean b=!m.findItem(R.id.nav_wall_events).isVisible();
                //setting submenus visible state
                m.findItem(R.id.nav_wall_events).setVisible(b);
                m.findItem(R.id.nav_wall_fun).setVisible(b);
                m.findItem(R.id.nav_wall_hadith).setVisible(b);
                m.findItem(R.id.nav_wall_news).setVisible(b);
                m.findItem(R.id.nav_wall_Poetry).setVisible(b);
                return true;
            } else if (id == R.id.nav_com) {
    
                m.findItem(R.id.nav_share).setVisible(false);
                m.findItem(R.id.nav_send).setVisible(false);
    
            } else if (id == R.id.nav_share) {
    
            } else if (id == R.id.nav_send) {
    
            }
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    

    where menu layout file is:

    
    
    
        
            
    
            
            
            
            
    
    
            
    
            
            
            
            
            
    
    
        
    
        
            
                
                
            
        
    
    
    

提交回复
热议问题