【问题标题】:BottomNavigationView icons not highlighted with Navigation 2.4未使用 SDK 31 突出显示的 BottomNavigationView 图标
【发布时间】:2022-02-25 09:21:34
【问题描述】:

我更新为compileSdkVersion 31,现在当点击BottomNavigationView 项目时,它并不总是突出显示图标或显示BottomNavigationView 项目ID 指向的片段。

底部有 3 个导航标签,分别称为 Home、Actions、My Progress。从Home 片段,您可以导航到SubFragment。

所以流程可能从Home 开始 --> 转到SubFragment --> 使用BottomNavigationView 转到Actions --> 然后点击Home 返回。在更新之前这样做是打开Home 片段(期望的行为)。现在,当点击主页图标时,它会显示SubFragment,并且不会突出显示该图标。

更多详情

这是导航控制器设置:

bottomNavigationView = findViewById(R.id.bottom_navigation_view)

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
bottomNavigationView.setupWithNavController(navController)

nav_graph 结构是这样的:

    <fragment
            android:id="@+id/home"
            android:name="com.example.app.home"
            android:label="Home"
            tools:layout="@layout/home" 

            <action
                android:id="@+id/action_home_fragment_to_sub_fragment"
                app:destination="@id/sub_fragment"/>
    </fragment>

    <fragment
            android:id="@+id/subfragment"
            android:name="com.example.app.subfragment"
            android:label="Sub Fragment"
            tools:layout="@layout/subfragment" />

    <fragment
            android:id="@+id/actions"
            android:name="com.example.app.actions"
            android:label="Actions"
            tools:layout="@layout/actions" />

    <fragment
            android:id="@+id/myprogress"
            android:name="com.example.app.myprogress"
            android:label="My Progress"
            tools:layout="@layout/myprogress" />

BottomNavigationView 的菜单项 ID 与 nav_graph 相同。

我认为问题可能在于nav_graph 结构不能很好地与新的 SDK 配合使用,因此我尝试调整nav_graph 的结构,以便每个导航选项卡都在其自己的like this question answer has setup 内。它使用这种方法突出显示选项卡,但仍不会像上面的示例那样导航到 Home。仅限SubFragment。

感谢我可能缺少的想法!

【问题讨论】:

  • Navigation 2.4 及更高版本支持multiple back stacks,因此预计重新选择“主页”选项卡将恢复该选项卡的整个后台堆栈(即,包括您的子片段)。
  • @ianhanniballake 感谢您的参与。您是说默认值已更改,并且通过重新选择“主页”选项卡不会转到主页片段(即 id BottomNavigationView 指向),而是转到该堆栈中的最后一个片段?有没有一种简单的方法可以在选择“主页”选项卡时转到“主页”片段?

标签: android kotlin bottomnavigationview android-navigation-graph


【解决方案1】:

尚未找到解决此问题的方法,因此降级为def nav_version = "2.3.5",导航再次正常工作。

这是上面 cmets 中 @ianhanniballake 提到的 Navigation 2.4 之前的版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多