Android on Drawer Closed Listener
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.
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.
setDrawerListener
is deprecated use addDrawerListener
instead.
drawerLayout.addDrawerListener(new DrawerListener() {
@Override
public void onDrawerSlide(View view, float v) {
}
@Override
public void onDrawerOpened(View view) {
}
@Override
public void onDrawerClosed(View view) {
}
@Override
public void onDrawerStateChanged(int i) {
}
});