close Navigation Drawer with no animation

邮差的信 提交于 2019-12-10 14:41:53

问题


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

回答1:


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.

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.




回答2:


You can consider setting animation time to 0. But it seems it's not as easy as setting one property. Here is tutorial and the code but it requires importing sources for DrawerLayout and ViewDragHelper into project and small modifications there.



来源:https://stackoverflow.com/questions/30130537/close-navigation-drawer-with-no-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!