我有一个使用导航抽屉的应用程序,它提供了位置列表。在抽屉中,有几个选项(如选择国家,城市等),用户可以在主要活动中显示相应列表之前进行设置。
当用户关闭抽屉时,是否有可能刷新列表,或者有其他方法可以解决这个问题?我试图搜索教程,但没有发现这个抽屉关闭的听众。 任何建议都会有所帮助,谢谢!
答案 0 :(得分:70)
当您设置ActionBarDrawerToggle
时,您可以“实施” onDrawerClosed
和onDrawerOpened
回调。请参阅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);
编辑:现在不推荐使用setDrawerListener,而是使用addDrawerListener。
答案 1 :(得分:37)
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) {
}
});
}
根据kit's comment,现在应该使用addDrawerListener()
,因为setDrawerListener()
已被弃用。
答案 2 :(得分:1)
setDrawerListener
已过时,请改用addDrawerListener
。
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) {
}
});