[英]How to update selected item when bottom navigation switch to Navigation Rail
I have a Bottom-Navigation in portrait Mode and a Navigation-Rail-View in landscape mode.我有纵向模式的底部导航和横向模式的导航轨道视图。
How can I update the selected item when the device is rotated?旋转设备时如何更新所选项目?
I tried it with savedInstanceState
, with override the onSaveInstanceState
and onRestoreInstanceState
.我尝试使用
savedInstanceState
,覆盖onSaveInstanceState
和onRestoreInstanceState
。
But because I use savedInstanceState
for other things too, overide the two above methods cause crash.但是因为我也将
savedInstanceState
用于其他事情,所以覆盖上述两种方法会导致崩溃。
Eventually I found a solution.最终我找到了解决方案。
But before that, I should mention that a better title for this question could be: How to share selection item between the BottomNavigation and NavigationRail.但在此之前,我应该提一下,这个问题的一个更好的标题可能是:如何在 BottomNavigation 和 NavigationRail 之间共享选择项。
Ok, solution:好的,解决方案:
Just replace setSelectedItemId
with getMenu().getItem(itemIndex).setChecked(true)
只需将
setSelectedItemId
替换为getMenu().getItem(itemIndex).setChecked(true)
Example:例子:
Use:利用:
bottomNavigationView.getMenu().getItem(indexOfItem2).setChecked(true);
Instead of:代替:
bottomNavigationView.setSelectedItemId(R.id.item2);
try this尝试这个
navigationRailView.setSelected(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.