Clicking hamburger icon on Toolbar does not open Navigation Drawer

后端 未结 13 2797
太阳男子
太阳男子 2020-12-14 00:08

I have a simple android.support.v7.widget.Toolbar and all I am trying to do is to open a NavigationDrawer by pressing the \"hamburger\" icon in the top left cor

13条回答
  •  温柔的废话
    2020-12-14 00:36

    Simply call function onOptionsItemSelected(MenuItem menuItem) of ActionBarDrawerToggle class on activity options menu managing function like below.

    mDrawerToggle= new ActionBarDrawerToggle(activity, drawerLayout, R.string.nav_drawer_accessbility_drawer_open,
                R.string.nav_drawer_accessbility_drawer_close);
    
    
     @Override
    public boolean onOptionsItemSelected(final android.view.MenuItem item) {
        navigation().getOptionsMenuInflater(this).closeSearchView();
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this behavior.
        mDrawerToggle.onOptionsItemSelected(item);
    
        return super.onOptionsItemSelected(item);
    }
    

    OR

    override onOptionsItemSelected of activity like below

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item != null && item.getItemId() == android.R.id.home) {
            toggle();
    
        }
        return super.onOptionsItemSelected(item);
    }
    
     private void toggle() {
        if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    }
    

提交回复
热议问题