简体   繁体   English

底部导航切换到导航栏时如何更新所选项目

[英]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 ,覆盖onSaveInstanceStateonRestoreInstanceState

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.

相关问题 发生选定错误时的底部导航活动 - Bottom Navigation Activity when selected error occured 选择ListView的项时如何关闭导航抽屉? - How to close the navigation drawer when an Item of ListView is selected? 如何停止点击相同的底部导航项? - How stop click on same bottom navigation item? 是否可以选择底部导航项目的圆形背景? - Is is possible to have rounded background of selected Bottom Navigation item? 如果在底部导航栏中选择了其他项目,如何删除 floatingActionButton 替换的片段 - How to remove floatingActionButton replaced fragment if other item is selected in bottom navigation bar 底部导航 在导航项上选择侦听器不起作用 - Bottom navigation On navigation item select listener not working 在底部导航栏中添加项目 - Add an item in bottom navigation bar 选择相同位置时禁用动画底部导航 - Disable animate bottom navigation when selected same position 从底部导航中选择时,如何阻止片段重新加载 WebView? - How do I stop fragments from reloading WebView when selected from bottom navigation? 如何一起使用底部导航栏和侧边导航栏,我的侧边导航按钮不显示时我但底部导航 - How to use bottom navigation bar and side navigation bar together, my side navigation button is not showing when i but bottom navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM