简体   繁体   English

导航抽屉打开时立即关闭

[英]Navigation Drawer closes instantly on open

I'm using the support library and the DrawerLayout. 我正在使用支持库和DrawerLayout。

The app starts with the drawer open as per android guidelines. 该应用程序开始按照Android准则打开抽屉。 If the drawer is closed and the user hits the back button I want the drawer to open. 如果抽屉是关闭的,并且用户点击了后退按钮,则我想打开抽屉。 I'm intercepting onKeyDown to open the drawer, but the drawer opens and closes instantly, resulting in a flicker. 我拦截了onKeyDown来打开抽屉,但是抽屉会立即打开和关闭,从而导致闪烁。

 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if(!drawerLayout.isDrawerOpen(drawerMenu)){
            drawerLayout.openDrawer(drawerMenu);
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Why not using OnBackPressd method? 为什么不使用OnBackPressd方法?

 @Override
public void onBackPressed() {

     if(!drawerLayout.isDrawerOpen(drawerMenu)){
     drawerLayout.openDrawer(drawerMenu);

    }
    else
    super.onBackPressed();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM