再次按下DrawerToggle之后的关闭抽屉



我想在再次按下切换后关闭导航图。现在,如果命中切换,它只是打开抽屉。选择项目后,我可以关闭抽屉。

是否可以使用我的方法实现,关闭抽屉?

那是我的方法:

@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成员

来跟踪是否打开

lemme尝试这样的东西

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

相关内容

  • 没有找到相关文章