Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android on Drawer Closed Listener

I have an application using navigation drawer that provides list of locations. In the drawer, there are several options (like choosing country, city, etc) that user can setup before showing the corresponding list in the main activity.

Is there any possibility to refresh the list when user close the drawer, or maybe there is another way to solve this? I've tried to search for tutorials but found nothing about this drawer closed listener. Any suggestions would be helpful, thanks!

like image 532
Ren Avatar asked Sep 28 '14 07:09

Ren


People also ask

What is ActionBarDrawerToggle?

androidx.appcompat.app.ActionBarDrawerToggle. This class provides a handy way to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.

What is Drawer view?

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. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.

What is drawer layout in Android Studio?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.


2 Answers

When you setup the ActionBarDrawerToggle you can "implement" the onDrawerClosed and onDrawerOpened callbacks. See the following example from the Docs:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,             R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {          /** Called when a drawer has settled in a completely closed state. */         public void onDrawerClosed(View view) {             super.onDrawerClosed(view);             // Do whatever you want here         }          /** Called when a drawer has settled in a completely open state. */         public void onDrawerOpened(View drawerView) {             super.onDrawerOpened(drawerView);             // Do whatever you want here         }     }; // Set the drawer toggle as the DrawerListener mDrawerLayout.addDrawerListener(mDrawerToggle); 

Edit: Now the setDrawerListener is deprecated, use addDrawerListener instead.

like image 167
reVerse Avatar answered Sep 24 '22 16:09

reVerse


reVerse answer is right in case you are using ActionBar as well. in case you just use the DrawerLayout directly, you can add a DrawerListener to it:

View drawerView = findViewById(R.id.drawer_layout); if (drawerView != null && drawerView instanceof DrawerLayout) {     mDrawer = (DrawerLayout)drawerView;     mDrawer.setDrawerListener(new DrawerListener() {             @Override             public void onDrawerSlide(View view, float v) {              }              @Override             public void onDrawerOpened(View view) {              }              @Override             public void onDrawerClosed(View view) {                 // your refresh code can be called from here             }              @Override             public void onDrawerStateChanged(int i) {              }         }); } 

As per kit's comment, addDrawerListener() should be used now that setDrawerListener() has been deprecated.

like image 40
Opster Elasticsearch Expert Avatar answered Sep 22 '22 16:09

Opster Elasticsearch Expert