Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event before DrawerLayout opens

It seems to me that one thing is missing from the DrawerLayout implementation: and event that fires before the drawer opens.

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
  public void onDrawerClosed(View view) {
    //...
  }

  public void onBeforeDrawerOpened(View drawerView) {
    //...
  }

  public void onDrawerOpened(View drawerView) {
    //...
  }
};

The reason for needing it would be to set up the status of the drawer items like menu items selected, enabled and similar. Putting this into the onDrawerOpened() callback kinda works but it doesn't look really right to change the menu appearance after it opens.

My first thought would be to extend ActionBarDrawerToggle and provide the new event. Shall I re-invent the wheel :-) or has this already been done by somebody?

like image 287
Gábor Avatar asked Dec 20 '22 02:12

Gábor


1 Answers

This link can help you to solve your problem. How to detect that the DrawerLayout started opening?

Use onDrawerStateChanged(int newState) callback

You need to listen to STATE_SETTLING states - this state is reported whenever drawer starts moving (either opens or closes). So once you see this state - check whether drawer is opened now and act accordingly:

    mDrawerToggle = new ActionBarDrawerToggle(
        this,                 
        mDrawerLayout,        
        R.drawable.ic_drawer,  
        R.string.drawer_open,  
        R.string.drawer_close  
) {
    @Override
    public void onDrawerStateChanged(int newState) {
        if (newState == DrawerLayout.STATE_SETTLING) {
            if (!isDrawerOpen()) {
                // starts opening
                getActionBar()
                        .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            } else {
                // closing drawer
                getActionBar()
                        .setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            }
            invalidateOptionsMenu();
        }
    }
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
like image 160
Fred Avatar answered Jan 02 '23 22:01

Fred