关闭不带动画的导航抽屉 [英] close Navigation Drawer with no animation
问题描述
我试图在完成某些活动后立即关闭导航抽屉,而没有滑动动画.我不会在活动开始时关闭它,因为如果用户退出活动,我希望它保持打开状态.但是,我无法在没有短暂闪烁的动画的情况下关闭它,因为它本身会关闭.我尝试了以下代码的变体:
I am trying to close my navigation drawer after I finish certain activities immediately, without the sliding animation. I don't close it on activity start because I would like it to stay open if the user backs out of the activity. However, I cannot get it to close without a brief flickering animation as it closes itself. I have tried variations of the following code:
protected void closeDrawerImmediate() {
mDrawerLayout.setVisibility(View.GONE);
mDrawerLayout.closeDrawers();
mDrawerLayout.setX(0);
mDrawerLayout.setVisibility(View.VISIBLE);
}
推荐答案
您可以在支持库的v24中使用新的DrawerLayout.closeDrawer(int/View, bool)
方法立即关闭抽屉:
You can use the new DrawerLayout.closeDrawer(int/View, bool)
methods in v24 of the support library to instantly close a drawer:
drawerLayout.closeDrawer(Gravity.LEFT, false);
如果您只是想立即关闭抽屉,例如从点击抽屉项启动活动时,这可能就足够了.
If you just want to close a drawer immediately, such as when you're launching an activity from tapping on a drawer item, that is probably enough.
如果您想在返回活动时关闭抽屉,则在适当的地方(例如开始新活动)设置一个像closeDrawerOnResume = true
这样的有状态布尔值,然后在onResume
中检查此布尔值并关闭抽屉,而不给它做动画.
If you want to close the drawer on coming back to the activity, I'd set a stateful boolean like closeDrawerOnResume = true
wherever is appropriate (eg. starting a new activity), and then in onResume
check this boolean and close the drawer without animating if it is true.
这篇关于关闭不带动画的导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!