Search code examples
androidandroid-actionbarslidingdrawer

ActionBarDrawerToggle event onClose when sliding


I'm implementing an activity with two action bar drawers.

Right now I have two buttons in the action bar which show these drawers and they are working fine. Even they close the other if already opened.

These drawers are intended to be used as filters (cities and categories) for a listview, and my idea is to listen for the OnClose event, so I can get the checked items and filter the list.

With "onDrawerClosed" I can do it very easy, but the problem is when drawers are shown via sliding, as the event is not fired.

I have been dealing then with "onDrawerSlide" and finally got this code:

void initDrawer() {
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_home_up, R.string.app_name,
            R.string.app_name) {

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            if (slideOffset == 0) {
                //if (!drawerLayout.isDrawerOpen(drawerView)) {
                    outputManager.showToast("Closed ");
                    //drawerClosed(drawerView);
                //}

                invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            super.onDrawerSlide(drawerView, slideOffset);
        }

        public void onDrawerClosed(View drawerView) {
            // getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
            drawerClosed(drawerView);
        }

        public void onDrawerOpened(View drawerView) {
            // getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

It kept firing the event a lot (I think it's fired several times until sliding is completed), so I checked if slideOffset is 0. It neither works, one of the sliders works, but the other doesn't fire any event.

Any idea? Maybe I could block sliding and allow to display drawers only via buttons, but how?


Solution

  • Finally I removed the second drawer and came up with a different approach. I think I tried to use it in a way is not intended for.