我有一个按this tutorial设计的导航抽屉。
当按下后退按钮时,应用程序退出。我想做的是关闭后按导航抽屉。这是原始代码:

drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);

我补充道:
public void onBackPressed() {
        if (this.drawerFragment.isDrawerOpen(GravityCompat.START)) {
            this.drawerFragment.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

但是,我得到的错误无法解决方法isDrawerOpencloseDrawer
我怎样才能让它工作?

最佳答案

我认为你不应该用碎片来打开和关闭抽屉。请尝试使用抽屉布局。

public void onBackPressed() {
    DrawerLayout layout = (DrawerLayout)findViewById(R.id.drawer_layout);
    if (layout.isDrawerOpen(GravityCompat.START)) {
        layout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

07-27 20:54