【问题标题】:Android Bottom Navigation with Navigation Component's NavigationUI change destination带有导航组件的 NavigationUI 更改目的地的 Android 底部导航
【发布时间】:2021-04-11 13:19:27
【问题描述】:

在使用喷气背包导航组件创建底部导航时,下面的代码将适用于活动

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
findViewById<BottomNavigationView>(R.id.bottom_nav).setupWithNavController(navController)

这样,在处理片段时,您需要调用 .setupWithNavController 并传入 navController,一切都应该没问题。

但在我的情况下,片段 XML 和底部导航中的导航图指定了应用程序构建,但它只停留在主屏幕中。

MainFragment.kt

class MainFragment : Fragment() {

    private var _binding: FragmentMainBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainBinding.inflate(inflater, container, false)
        NavigationUI.setupWithNavController(binding.bottomNav,findNavController())

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainFragment">


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_menu" />

    <fragment
        android:id="@+id/main_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

main_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/main_gragh"
    app:startDestination="@id/homeFragment">
    <fragment
        android:id="@+id/homeFragment"
        android:name="io.github.jerrymatera.medstab.ui.home.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/appointmentFragment"
        android:name="io.github.jerrymatera.medstab.ui.appointment.AppointmentFragment"
        android:label="appointment_fragment"
        tools:layout="@layout/appointment_fragment" />
    <fragment
        android:id="@+id/chatFragment"
        android:name="io.github.jerrymatera.medstab.ui.chat.ChatFragment"
        android:label="chat_fragment"
        tools:layout="@layout/chat_fragment" />
    <fragment
        android:id="@+id/profileFragment"
        android:name="io.github.jerrymatera.medstab.ui.profile.ProfileFragment"
        android:label="profile_fragment"
        tools:layout="@layout/profile_fragment" />
</navigation>

menu/bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/mainFragment"
        android:icon="@drawable/ic_home_24"
        android:title="@string/home" />
    <item
        android:id="@+id/appointmentFragment"
        android:enabled="true"
        android:icon="@drawable/ic_date_range_24"
        android:title="@string/appointments" />
    <item
        android:id="@+id/chatFragment"
        android:enabled="true"
        android:icon="@drawable/ic_chat_24"
        android:title="@string/chat" />
    <item
        android:id="@+id/profileFragment"
        android:enabled="true"
        android:icon="@drawable/ic_account_box_24"
        android:title="@string/profile" />
</menu>

【问题讨论】:

    标签: android kotlin android-jetpack-navigation


    【解决方案1】:

    在 NavigationView (menu/bottom_navigation_menu.xml) 的菜单资源中,每个项目 ID 必须与导航图中的片段 ID (main_graph.xml) 匹配。通过这个NavigationUI会计算出item和destination之间的映射,它会在item选择上执行fragment transaction。

    改变 android:id="@+id/mainFragment"android:id="@+id/homeFragment"menu/bottom_navigation_menu.xml

    【讨论】:

      【解决方案2】:

      也许问题出在MainFragment.kt,但我不知道确切原因

      NavigationUI.setupWithNavController(binding.bottomNav,findNavController())
      

      需要 BottomNavigationView 和 NavController

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        相关资源
        最近更新 更多