Android BottomNavigationView ViewModel 以编程方式设置状态

发布于 2025-01-11 05:58:07 字数 1543 浏览 2 评论 0原文

我正在尝试将底部工作表导航状态移动到视图模型。这将使我能够以编程方式设置选项卡,并在旋转时保存选项卡状态。

在视图模型中: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2025-01-18 05:58:07

您可以将选项卡标记为已选中,而无需设置所选项目,因此不会调用所选项目侦听器,如下所示:

bottomNavigationView.getMenu().getItem(0).setChecked(true);

You can mark tabs as checked without setting the selected item and therefore not calling the selected item listener as follows:

bottomNavigationView.getMenu().getItem(0).setChecked(true);
无人接听 2025-01-18 05:58:07

你可以考虑这样做:

private void onNavigationTab(NavTab tab) {
   bottomNavigationView.setOnItemSelectedListener(null)
   bottomNavigationView.setSelectedItemId(desired_id)
   bottomNavigationView.setOnItemSelectedListener(listener)
}

you may consider doing this:

private void onNavigationTab(NavTab tab) {
   bottomNavigationView.setOnItemSelectedListener(null)
   bottomNavigationView.setSelectedItemId(desired_id)
   bottomNavigationView.setOnItemSelectedListener(listener)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文