[英]How to prevent opening a fragment in navigation drawer if that fragment is already opened
如果已经打开碎片,如何防止在导航抽屉中打开该碎片。 例如,我首先在Nav
抽屉中有一个“设置”片段,如果单击该片段,它会打开,因此如果我单击它,现在应该再次打开,它不应两次打开相同的片段,因为这会在单击“后退”按钮时引起问题。
您可以保持片段在当前打开的列表中的位置。 例如,维护一个名为mSelectedPosition
的变量,该变量在onCreate()
初始化为-1。 然后,在onItemClick()
中的onItemClick()
中的DrawerItemClickListener
可能类似于
if (position == mSelectedPosition) {
closeDrawer();
return;
}
mSelectedPosition = position;
// Continue your work here.
可能的话,您应该在onDrawerClosed()回调中更改新的Fragment。 维护一个成员变量,例如currentFragmentPosition。
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
if(clickedPosition != currentFragmentPosition)
{
// change Fragment
}
currentFragmentPosition = clickedPosition;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.