带有导航组件的 NavigationUI 更改目的地的 Android 底部导航

时间:2021-04-11 13:19:27

标签: android kotlin android-jetpack-navigation

在使用 jetpack 导航组件创建底部导航时,以下代码适用于活动

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>

1 个答案:

答案 0 :(得分:0)

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

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

需要 BottomNavigationView 和 NavController