[英]How to prevent opening a fragment in navigation drawer if that fragment is already opened
How to prevent opening a fragment in navigation drawer if that fragment is already opened. 如果已经打开碎片,如何防止在导航抽屉中打开该碎片。 For example I have a Settings fragment in my
Nav
Drawer at first if click on that frag it opens so now again if I click on that it should not open the same fragment twice as this causes a problem while hitting back button. 例如,我首先在
Nav
抽屉中有一个“设置”片段,如果单击该片段,它会打开,因此如果我单击它,现在应该再次打开,它不应两次打开相同的片段,因为这会在单击“后退”按钮时引起问题。
You can maintain the position of the fragment in the list that is currently open. 您可以保持片段在当前打开的列表中的位置。 For instance, maintain a variable called
mSelectedPosition
, which is initialized to -1 in onCreate()
. 例如,维护一个名为
mSelectedPosition
的变量,该变量在onCreate()
初始化为-1。 Then the code in your onItemClick()
in the DrawerItemClickListener
can be something like 然后,在
onItemClick()
中的onItemClick()
中的DrawerItemClickListener
可能类似于
if (position == mSelectedPosition) {
closeDrawer();
return;
}
mSelectedPosition = position;
// Continue your work here.
Possibly, you should change the new Fragment at the onDrawerClosed() callback. 可能的话,您应该在onDrawerClosed()回调中更改新的Fragment。 Maintain a member variable like, currentFragmentPosition.
维护一个成员变量,例如currentFragmentPosition。
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
if(clickedPosition != currentFragmentPosition)
{
// change Fragment
}
currentFragmentPosition = clickedPosition;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.