从某个项目按下时如何关闭导航抽屉?


Answers:


235

得到它了!

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

工作完美。


2
询问哪个值得。
AXSM

3
可以不使用动画关闭它吗?
Hamzeh Soboh,2015年

1
Hamzeh-mDrawerLayout.closeDrawer(Gravity.LEFT,false); 会导致其关闭而没有动画
Psi-Ed

mDrawerLayout.closeDrawer(GravityCompat.START,false);
user3560827 '18

10

对我来说,这个有效-

mDrawerLayout.closeDrawer(Gravity.START, false);

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

2

在开关案例结束处的onNavigationItemSelected的右下角,您应该对此进行纠正。mDrawerLayout.closeDrawers();

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){

        }
   mDrawerLayout.closeDrawers();
   return true;
}

2

如果将mDrawerLayout作为抽屉式布局,则可以在打开时将其关闭。

@Override
public void onBackPressed() {
    if (this.mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        this.mDrawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

0
closeDrawer();//when you want to call

public void closeDrawer() {
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.