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

问题描述 投票:0回答:1

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

我正在尝试在一个活动中使用其中三个时遇到问题,我收到此错误:

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>
android navigation android-navigation android-architecture-navigation jitpack
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.