gpt4 book ai didi

Android导航组件: BottomNavigationView's selected tab icon is not updated

转载 作者:行者123 更新时间:2023-12-04 23:37:52 24 4
gpt4 key购买 nike

我正在使用 BottomNavigationView带有导航组件。当显示 fragment 不是根 fragment 时,选项卡图标不会更新(选中)。
例子:
当我在带有 fragment A(它是根 fragment )的 Tab Home 和带有 fragment B(也是根 fragment )的 Tab Star 之间切换时,它工作正常。
但是,当我从 Tab Home 导航到另一个 fragment (如 fragment A2)并点击 Tab Star 并再次返回 Tab Home 时,仍然在 BottomNavigationView 中选择了 Tab Star .
Android Navigation Component Bug
它在版本 2.4.0-alpha05 上运行良好, 当我将其更新为 2.5.0-alpha01 时发生这种情况.
build.gradle(应用程序)

implementation "androidx.navigation:navigation-fragment-ktx:2.5.0-alpha01"
implementation "androidx.navigation:navigation-ui-ktx:2.5.0-alpha01"
implementation "androidx.navigation:navigation-dynamic-features-fragment:2.5.0-alpha01"
build.gradle(根)
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha01"
图形:
Android Navigation Graph
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/graph"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="ui.test.FragmentA"
tools:layout="@layout/fragment_test"
android:label="FragmentA" >
<action
android:id="@+id/action_fragmentA_to_fragmentA2"
app:destination="@id/fragmentA2" />
</fragment>
<fragment
android:id="@+id/fragmentA2"
android:name="ui.test.FragmentA2"
tools:layout="@layout/fragment_test"
android:label="FragmentA2" />
<fragment
android:id="@+id/fragmentB"
android:name="ui.test.FragmentB"
tools:layout="@layout/fragment_test"
android:label="FragmentB" />
</navigation>
菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fragmentA"
android:icon="@drawable/ic_home"
android:title="" />
<item
android:id="@+id/fragmentB"
android:icon="@drawable/ic_star"
android:title="" />
</menu>
难道我做错了什么?或者这是错误?
我该如何解决这个问题?

最佳答案

所以对我有用的是 ianhanniballake 在他的回答中暗示的解决方案:使用 setOnItemSelectedListener。

// always show selected Bottom Navigation item as selected (return true)
bottomNavigationView.setOnItemSelectedListener { item ->
// In order to get the expected behavior, you have to call default Navigation method manually
NavigationUI.onNavDestinationSelected(item, navController)

return@setOnItemSelectedListener true
}
这将始终选择项目并导航到关联的目的地,同时保持多个后退堆栈。

关于Android导航组件: BottomNavigationView's selected tab icon is not updated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71089052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com