首页 > 解决方案 > 在片段内使用导航主机片段和底部导航视图?

问题描述

我在主页片段中有导航主机片段和底部导航视图,如下所示

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="home.HomeFragment2">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/homeNavHost"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/bottomMenu"
    app:layout_constraintEnd_toEndOf="parent"
    app:navGraph="@navigation/staging_menu_navigation1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomMenu"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_menu" />
 </androidx.constraintlayout.widget.ConstraintLayout>

菜单如

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/staging_dashboard_nav_graph"
    android:icon="@drawable/ic_home"
    android:title="@string/menu_dashboard" />
<item
    android:id="@+id/staging_offer_nav_graph"
    android:icon="@drawable/ic_offer"
    android:title="@string/menu_offers" />
<item
    android:id="@+id/staging_profile_nav_graph"
    android:icon="@drawable/ic_profile"
    android:title="@string/menu_profile" />
</menu>

导航图为

<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/staging_menu_navigation"
app:startDestination="@id/dashboardFragment3">

<include app:graph="@navigation/staging_dashboard_nav_graph" />
<include app:graph="@navigation/staging_offer_nav_graph" />
<include app:graph="@navigation/staging_profile_nav_graph" />
<fragment
    android:id="@+id/dashboardFragment3"
    android:name="com.octave.dashboard.DashboardFragment"
    android:label="DashboardFragment" />

</navigation>

其他图表如下

<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/staging_dashboard_nav_graph"
app:startDestination="@id/dashboardFragment">

<fragment
    android:id="@+id/dashboardFragment"
    android:name="com.octave.dashboard.DashboardFragment"
    android:label="dashboard_fragment"
    tools:layout="@layout/dashboard_fragment" />
</navigation>

报价图

<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/staging_offer_nav_graph"
app:startDestination="@id/offersFragment">

<fragment
    android:id="@+id/offersFragment"
    android:name="com.octave.offers.OffersFragment"
    android:label="offers_fragment"
    tools:layout="@layout/offers_fragment" />
</navigation>

剖面图

<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/staging_profile_nav_graph"
app:startDestination="@id/profileFragment">

<fragment
    android:id="@+id/profileFragment"
    android:name="com.octave.profile.ProfileFragment"
    android:label="profile_fragment"
    tools:layout="@layout/profile_fragment" />

在我的家庭片段中,我设置为

binding.bottomMenu.setupWithNavController(Navigation.findNavController(requireActivity(), R.id.homeNavHost))

当我点击底部菜单项时,片段不会改变。这里有什么问题?

标签: androidnavigationcontrollerandroid-navigationviewandroid-navigation-graphandroid-bottomnavigationview

解决方案


我希望这还不算太晚。我花了一天时间研究如何使用嵌套导航图。如果您尝试使用已经是默认导航图一部分的底部导航栏创建另一个导航图,您首先需要在您的导航主机片段所在的 XML 中将 defaultNaveHost 值更改为 false。然后找到 NavHost 到获取导航控制器

val navHost = childFragmentManager.findFragmentById(R.id.${yourNavHostFragment} as NavHostFragment
val navController = localNavHost.navController
view.findViewById<BottomNavigationView>(R.id.bottomNavigationView).setupWithNavController(navController!!)


推荐阅读