Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if the NavigationDrawer is opening or closing

Is there any way for me to determine whether the navigation drawer is opening or closing? I have read about isDrawerOpen() and isDrawerVisible() methods, but they only return true if the navigation drawer is opened or is visible respectively.

I also read about onDrawerSlide(View drawerView, float slideOffset) method which gets called when the drawer is moving. The slideOffset is the floating value between 0 and 1 telling which position the drawer is at now. But even that gets called when the drawer is opening and closing.

What I need to do is, to do something only when the drawer is opening but not when closing, something tells me that I have to use onDrawerSlide(View drawerView, float slideOffset) method, but I just can't figure out how to check if it is opening and is not closing.

Thank you

like image 249
Sudara Avatar asked Mar 23 '14 12:03

Sudara


People also ask

Which piece of code is used to close the navigation drawer?

Just add closeDrawer() inside onNavigationItemSelected() to close the drawer on selecting any item on Navigation Drawer.

How does navigation drawer work?

The navigation drawer slides in from the left and contains the navigation destinations for the app. The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar.


1 Answers

To track the last value is an option ...

drawerLayout.setDrawerListener(new DrawerListener() {

        private float last = 0;

        @Override
        public void onDrawerSlide(View arg0, float arg1) {

            boolean opening = arg1>last;
            boolean closing = arg1<last;

            if(opening) {
                Log.i("Drawer","opening");
            } else if(closing) {
                Log.i("Drawer","closing");
            } else {
                Log.i("Drawer","doing nothing");
            }

            last = arg1;
        }

        @Override public void onDrawerStateChanged(int arg0) {}
        @Override public void onDrawerOpened(View arg0) {}
        @Override public void onDrawerClosed(View arg0) {}

    });
like image 63
ElDuderino Avatar answered Oct 24 '22 21:10

ElDuderino