导航到其他片段、切换到其他菜单并切换回初始菜单后,BottomNavigationView 的菜单未选择

Owe*_*Lie 9 kotlin android-studio android-navigation android-bottomnav

我正在使用底部导航构建一个带有 3 个菜单的 Android 应用程序。我使用底部导航活动在 Android Studio 中创建了新项目。

我将该片段重命名为:

  • InfoFragment.kt

  • DetectFragment.kt

  • AboutFragment.kt

将布局重命名src/main/res/layout为:

  • fragment_info.xml

  • fragment_detect.xml

  • fragment_about.xml

将菜单重命名src/main/res/menu为:

  • navigation_info

  • navigation_detect

  • navigation_about

fragment_about.xml我添加了一个按钮buttonGoToFAQ来导航到fragment_faq 这样的代码AboutFragment.kt

buttonGoToFAQ.setOnClickListener {
        val action = AboutFragmentDirections.actionFAQ()
        Navigation.findNavController(it).navigate(action)
    }
Run Code Online (Sandbox Code Playgroud)

单击 BottomNavigationView 菜单 或 后navigation_infonavigation_detect然后单击navigation_about菜单返回,BottomNavigationView 上的选定菜单不会更改。
看这张图

我想要的是应该选择菜单navigation_about而不是其他菜单。

我已经尝试过覆盖fun onStart()fun onResume()进入FAQFragment.kt但无济于事。
nav_view是我的 BottomNavigationView。

override fun onStart() {
    super.onStart()

    (requireActivity().findViewById<View>(R.id.nav_view) as BottomNavigationView).selectedItemId =
        R.id.navigation_about
}
Run Code Online (Sandbox Code Playgroud)

src/main/res/navigation我还认识到所有 BottomNavigationView 菜单的 id 与xml 文件中的 id 具有相同的 id

小智 8

根据 Google 问题跟踪器的链接 - https://issuetracker.google.com/issues/210687967?pli=1 ,这是预期的行为

简而言之,Google 希望我们使用嵌套导航图来链接FaqFragmentAboutFragment。这将导致navigation_about每当打开任一片段时都会选择菜单。

  1. 在您的中nav_graph.xml,添加一个嵌套图 -
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
    app:startDestination="@id/infoFragment">

<fragment
    android:id="@+id/infoFragment"
    android:name="......"
    android:label="..." >
        ....
</fragment>

<fragment
    android:id="@+id/detectFragment"
    android:name="......"
    android:label="...." >
        ....
</fragment>  

<navigation android:id="@+id/toAboutNav"
    app:startDestination="@id/aboutFragment">
    <fragment
        android:id="@+id/aboutFragment"
        android:name="....."
        android:label="....." >
        .......
    </fragment>

    <fragment
        android:id="@+id/faqFragment"
        android:name="...."
        android:label="...">
        ....
    </fragment>
</navigation>
</navigation>
Run Code Online (Sandbox Code Playgroud)
  1. 另外,在您的菜单文件夹中,进行以下更改bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@id/infoFragment"
        android:icon="...."
        android:title="...." />
    <item
        android:id="@id/detectFragment"
        android:icon="...."
        android:title="...." />
    <item
        android:id="@id/toAboutNav"
        android:icon="...."
        android:title="...." />
</menu>
Run Code Online (Sandbox Code Playgroud)


Owe*_*Lie 6

几天后,我终于自己得到了答案。首先,我需要获取BottomNavigationViewfrom MainActivity,之后,您可以从另一个片段更改菜单项值。

MainActivity.kt

companion object {
    lateinit var binding: ActivityMainBinding
{
Run Code Online (Sandbox Code Playgroud)

在片段上,
定义BottomNavigationView并设置所需的索引onResume()

class FAQFragment : Fragment() {
    private val navView: BottomNavigationView = MainActivity.binding.navView

    ...

    override fun onResume() {
        super.onResume()

        navView.menu.getItem(2).isChecked = true
    }
}
Run Code Online (Sandbox Code Playgroud)


Fra*_*cis 6

您可以手动导航并始终返回trueOnItemSelectedListener实现菜单项选择。

binding.bottomNavigation.setOnItemSelectedListener { item ->
    NavigationUI.onNavDestinationSelected(item, navController)
    true
}
Run Code Online (Sandbox Code Playgroud)