如何在一个活动中拥有多个navHostFragment? [英] How can I have more than one navHostFragment in one activity?

查看:32
本文介绍了如何在一个活动中拥有多个navHostFragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在一个活动中有三个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>

推荐答案

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

这篇关于如何在一个活动中拥有多个navHostFragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆