Clicking hamburger icon on Toolbar does not open Navigation Drawer

此生再无相见时 提交于 2019-11-27 09:41:25

You're using the four-parameter constructor for ActionBarDrawerToggle, which means you'll have to call the toggle's onOptionsItemSelected() method in MainActivity's onOptionsItemSelected() override in order to open/close the drawer.

For example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

If you happen to be providing your own Toolbar – e.g., as the support ActionBar (though it's not necessary to set it as such) – then you can instead pass that Toolbar as the third argument in the ActionBarDrawerToggle constructor call. For example:

Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
        toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

The drawer opening/closing will then be handled by ActionBarDrawerToggle internally, and you won't need to call through to the toggle in onOptionsItemSelected().

The setDisplayHomeAsUpEnabled() call is also unnecessary for this setup, which is handy if you don't want to set the Toolbar as the ActionBar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!