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

丶灬走出姿态 提交于 2019-12-03 06:27:58

问题


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


回答1:


Got it!

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

Working perfectly fine.




回答2:


For me this one worked -

mDrawerLayout.closeDrawer(Gravity.START, false);



回答3:


closeDrawer(); // called when you want to close

public void closeDrawer() {
  if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
    mDrawerLayout.closeDrawer(GravityCompat.START);
  }
}



回答4:


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


来源:https://stackoverflow.com/questions/19194526/how-to-close-navigation-drawer-when-an-item-is-pressed-from-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!