如何在一项活动中拥有多个navHostFragment?

时间:2019-04-01 11:57:20

标签: android navigation android-navigation android-architecture-navigation jitpack

我想在一个活动中拥有三个navHostFragments,它们都占用相同的空间,但是每个宿主一个不同的导航图。然后,使用片段管理器和底部导航栏,在需要时显示每个窗口(并隐藏其他窗口),并以此保持用户在每个图中的当前位置。

当我一次尝试让其中3个参加活动时,我目前遇到问题,并且出现此错误:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #38: Duplicate id 0x7f0a0086, tag container_fragment, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FeedActivity">




    <androidx.appcompat.widget.Toolbar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:theme="@style/MyActionBar"
            android:minHeight="?attr/actionBarSize"
            android:id="@+id/my_toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


    <fragment
            android:id="@+id/feed_nav_host_fragment"
            android:tag="container_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_height="0dp"
            app:defaultNavHost="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/feed_bottom_nav"
            app:navGraph="@navigation/feed_nav_graph"
            android:layout_width="0dp"
            app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
    />

    <fragment
            android:id="@+id/board_nav_host_fragment2"
            android:tag="container_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_height="0dp"
            app:defaultNavHost="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/feed_bottom_nav"
            app:navGraph="@navigation/board_nav_graph"
            android:layout_width="0dp"
            app:layout_constraintTop_toBottomOf="@+id/my_toolbar"

    />

    <fragment
            android:id="@+id/profile_nav_host_fragment3"
            android:tag="container_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_height="0dp"
            app:defaultNavHost="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/feed_bottom_nav"
            app:navGraph="@navigation/current_user_profile_nav_graph"
            android:layout_width="0dp"
            app:layout_constraintTop_toBottomOf="@+id/my_toolbar"

    />


    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/feed_bottom_nav"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:itemBackground="@color/white"
            app:itemTextColor="@color/bottom_nav_color"
            app:itemIconTint="@color/bottom_nav_color"
            app:menu="@menu/navigation"
            app:labelVisibilityMode="unlabeled"/>

</androidx.constraintlayout.widget.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

我建议您查看Google的Android体系结构组件示例,尤其是Advanced Navigation Sample,因为它显示了如何使用Navigation和BottomNavigationView处理多个后向堆栈