How to open Side bar on icon click in Android?

前端 未结 3 1423
小鲜肉
小鲜肉 2021-01-07 04:52

I have implemented Hamburger bar with App toolbar and both of them are working fine. Following is the snapshot of toolbar and ha

3条回答
  •  死守一世寂寞
    2021-01-07 05:19

    Use Activity's onOptionsItemSelected(MenuItem menuItem) method:

    First of all, keep the reference to your DrawerLayout in a class field:

    DrawerLayout drawerLayout;
    

    Somewhere in onCreate put this:

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)
    

    And implement the method:

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        // if you want the default back/home hamburger menu item just put android.R.id.home instead
        if (menuItem.getItemId() == R.drawable.icon_navigation) { 
            drawerLayout.openDrawer(GravityCompat.END);
        }
        return super.onOptionsItemSelected(menuItem);
    }
    

提交回复
热议问题