简体   繁体   English

在片段内使用导航主机片段和底部导航视图?

[英]Using nav host fragment and bottom navigation view inside fragment?

I have nav host fragment and bottom navigation view inside home fragment as below我在主页片段中有导航主机片段和底部导航视图,如下所示

<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.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>

The menu as菜单如

<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>

The navigation graph as导航图为

<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>

The other graphs are as其他图表如下

<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>

Offer graph报价图

<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>

Profile graph剖面图

<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" />

In my home fragment I am setting up as在我的家庭片段中,我设置为

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

When I tap on bottom menu items the fragment doesn't change.当我点击底部菜单项时,片段不会改变。 What is wrong over here?这里有什么问题?

I hope this is not too late.我希望这还不算太晚。 I am been spending a day figuring out how to work with the nested nav-graph.我花了一天时间研究如何使用嵌套导航图。 If you are trying to create another nav graph with the bottom navigation bar that is already a part of the default nav graph, you first need to change the defaultNaveHost value to false in XML of wherever your nav host fragment is in. then find NavHost to get NavController如果您尝试使用已经是默认导航图的一部分的底部导航栏创建另一个导航图,则首先需要将导航主机片段所在位置的 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!!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM