Android BottomNavigationView ViewModel 以编程方式设置状态
我正在尝试将底部工作表导航状态移动到视图模型。这将使我能够以编程方式设置选项卡,并在旋转时保存选项卡状态。
在视图模型中: enum class NavTab { TAB_1, TAB_2, TAB_3 }
val navigationTab: LiveData<NavigationTab> = savedStateHandle.getLiveData("nav_tab")
fun setNavigationTab(tab: NavigationTab) {
savedStateHandle.set("nav_tab", tab)
}
在活动中:
bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.tab_1:
viewModel.setNavigationTab(NavTab.TAB_1);
break;
case R.id.tab_2:
viewModel.setNavigationTab(NavTab.TAB_2);
break;
case R.id.tab_3:
viewModel.setNavigationTab(NavTab.TAB_3);
break;
}
return true;
});
viewModel.getNavigationTab().observe(this, this::onNavigationTab);
private void onNavigationTab(NavTab tab) {
switch (tab) {
case TAB_1:
bottomNavigationView.setSelectedItemId(R.id.tab_1);
break;
case TAB_2:
bottomNavigationView.setSelectedItemId(R.id.tab_2);
break;
case TAB_3:
bottomNavigationView.setSelectedItemId(R.id.tab_3);
break;
}
}
但是,问题是通过调用 setNavigationTab 以编程方式设置选项卡时,只有片段正在切换,选项卡未选择属性。我可以调用
bottomNavigationView.setSelectedItemId(R.id.tab_2);
但是如果我这样做,视图模型将看到更改并触发观察者,从而导致无限循环。
在以编程方式更改选项卡的情况下,我需要切换片段和 UI 选项卡。如果用户单击选项卡,我只需要设置片段,因为选项卡 UI 会相应更改。
I am trying to move my bottom sheet navigation state to a viewmodel. This will enable me to set the tab programmatically and also save the tab state on rotation.
In the viewmodel:
enum class NavTab { TAB_1, TAB_2, TAB_3 }
val navigationTab: LiveData<NavigationTab> = savedStateHandle.getLiveData("nav_tab")
fun setNavigationTab(tab: NavigationTab) {
savedStateHandle.set("nav_tab", tab)
}
In the activity:
bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.tab_1:
viewModel.setNavigationTab(NavTab.TAB_1);
break;
case R.id.tab_2:
viewModel.setNavigationTab(NavTab.TAB_2);
break;
case R.id.tab_3:
viewModel.setNavigationTab(NavTab.TAB_3);
break;
}
return true;
});
viewModel.getNavigationTab().observe(this, this::onNavigationTab);
private void onNavigationTab(NavTab tab) {
switch (tab) {
case TAB_1:
bottomNavigationView.setSelectedItemId(R.id.tab_1);
break;
case TAB_2:
bottomNavigationView.setSelectedItemId(R.id.tab_2);
break;
case TAB_3:
bottomNavigationView.setSelectedItemId(R.id.tab_3);
break;
}
}
However the problem is when setting the tab programmatically by calling setNavigationTab only the fragment is switching the tab is not property selected. I can call
bottomNavigationView.setSelectedItemId(R.id.tab_2);
However if I do this, the view model will see the change and fire the observer, causing an infinite loop.
In the case of programmatically changing the tab I need to switch the fragment and the UI tab. In case of user clicking the tab, I only need to set the fragment as the tab UI is changed appropriately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将选项卡标记为已选中,而无需设置所选项目,因此不会调用所选项目侦听器,如下所示:
You can mark tabs as checked without setting the selected item and therefore not calling the selected item listener as follows:
你可以考虑这样做:
you may consider doing this: