8

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

Sudara
  • 4,769
  • 3
  • 34
  • 39

3 Answers3

18

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) {}

    });
ElDuderino
  • 3,253
  • 2
  • 21
  • 38
0

If You are using ActionBar ovveride ActionBarDrawerToggle

Further reading: Listen for Open and Close Events on developer.android.com

pixel
  • 24,905
  • 36
  • 149
  • 251
0

The setDrawerListener method is now deprecated. The new way to do this is to use the addDrawerListener method.

In Kotlin:

  1. Initialize the DrawerLayout View

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

  2. In your main activity, create an inner class that is a subclass of DrawerLayout.DrawerListener. The DrawerLayout class implements the DrawerListener interface. You can track the changing drawer position in onDrawerSlide (Kotlin-ized version of answer above):

    inner class CustomDrawer : DrawerLayout.DrawerListener{
      override fun onDrawerStateChanged(newState: Int) {
      }
    
    var last: Float = 0f
    
    override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
    
        var opening: Boolean = slideOffset>last
        var closing: Boolean = slideOffset<last
    
        if(opening) {
            Log.i("Drawer","opening")
            Log.d("Last",last.toString())
        } else if(closing) {
            Log.i("Drawer","closing")
            Log.d("Last",last.toString())
        } else {
            Log.i("Drawer","doing nothing")
            Log.d("Last",last.toString())
        }
    }
    
    override fun onDrawerClosed(drawerView: View) {
    
      }
    
    override fun onDrawerOpened(drawerView: View) {
    
      }
    }
    
  3. Add your custom DrawerListener to the drawerLayout (I put it in the onCreate method)

    var drawerListener = CustomDrawer() drawerLayout.addDrawerListener(drawerListener)

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61