How to close navigation drawer when an item is pressed from it?

前端 未结 6 1101
日久生厌
日久生厌 2021-02-01 11:46

Ideally navigation drawer should get closed once some item has been pressed from it, but its not happening automatically. How to do it ? Thanks!

相关标签:
6条回答
  • 2021-02-01 12:13

    For me this one worked -

    mDrawerLayout.closeDrawer(Gravity.START, false);
    
    0 讨论(0)
  • 2021-02-01 12:15

    Got it!

    private DrawerLayout mDrawerLayout;
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.closeDrawers();
    

    Working perfectly fine.

    0 讨论(0)
  • 2021-02-01 12:20

    On the right bottom of onNavigationItemSelected where the switch case ends you should right this. mDrawerLayout.closeDrawers();

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
    
            }
       mDrawerLayout.closeDrawers();
       return true;
    }
    
    0 讨论(0)
  • 2021-02-01 12:25
    closeDrawer();//when you want to call
    
    public void closeDrawer() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }
    }
    
    0 讨论(0)
  • 2021-02-01 12:29
    DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout)
    
    closeDrawer(); // called when you want to close
    
    public void closeDrawer() {
      if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
      }
    }
    
    0 讨论(0)
  • 2021-02-01 12:31

    If you have mDrawerLayout as your drawer layout, you can close it when it is open.

    @Override
    public void onBackPressed() {
        if (this.mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            this.mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
提交回复
热议问题