再次按下Drawtoggle后关闭抽屉

我想在关闭切换按钮后关闭导览器。现在,只要打开切换键,它就会打开抽屉。选择一个项目后,我可以关闭抽屉。再次按下Drawtoggle后关闭抽屉

是否可以执行我的方法来关闭抽屉?

这是我的方法:

@Override 

public boolean onCreateOptionsMenu(Menu menu) {

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case android.R.id.home:

mDrawerLayout.openDrawer(GravityCompat.START); //THIS OPENS THE DRAWER

return true;

}

return super.onOptionsItemSelected(item);

}

private void setupDrawerContent(NavigationView navigationView) {

navigationView.setNavigationItemSelectedListener(

new NavigationView.OnNavigationItemSelectedListener() {

@Override

public boolean onNavigationItemSelected(MenuItem menuItem) {

menuItem.setChecked(true);

mDrawerLayout.closeDrawers(); //THIS CLOSES THE DRAWER; AFTER A ITEM IS SELECTED

return true;

}

});

}

回答:

易我猜。只要保持它是否是开放或不引入新public boolean isDrawerOpen成员的轨道

让我再尝试这样的事情

@Override 

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case android.R.id.home:

if (!isDrawerOpen) {

mDrawerLayout.openDrawer(GravityCompat.START);

isDrawerOpen = true;

} else {

mDrawerLayout.closeDrawers();

isDrawerOpen = false;

}

return true;

}

return super.onOptionsItemSelected(item);

}

private void setupDrawerContent(NavigationView navigationView) {

navigationView.setNavigationItemSelectedListener(

new NavigationView.OnNavigationItemSelectedListener() {

@Override

public boolean onNavigationItemSelected(MenuItem menuItem) {

menuItem.setChecked(true);

mDrawerLayout.closeDrawers(); //THIS CLOSES THE DRAWER; AFTER A ITEM IS SELECTED

isDrawerOpen = false;

return true;

}

});

}

以上是 再次按下Drawtoggle后关闭抽屉 的全部内容, 来源链接: utcz.com/qa/266842.html

回到顶部