这个页面有一个左侧导航栏,当用户选择其中一项时,右侧的片段会发生变化。问题在于,如果“项目A”已经被选中,用户再次选择该项时,片段仍然会改为相同的片段。
为了避免这种行为,我需要知道哪个项目已经被选中。我之前使用了一个属性mLastSelectedItemPosition,但我认为这不是一个好的解决方案。是否有任何方法从导航抽屉中获取当前已选中的项目?
class OnNavigationItemSelectedListener implements NavigationView.OnNavigationItemSelectedListener {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (mLastSelectedItemPosition != menuItem.getOrder()) {
// Is not the same item, so I can change the fragment.
mLastSelectedItemPosition = menuItem.getOrder();
}
mDrawerLayout.closeDrawers();
return true;
}
}
}