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

2024-02-02

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

我目前在尝试在一项活动中使用其中 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>

我建议查看 Google 的 Android 架构组件示例,尤其是高级导航示例 https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample因为它展示了如何使用导航和 BottomNavigationView 处理多个后退堆栈

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在一项活动中拥有多个 navHostFragment? 的相关文章

  • 用于 C# XNA 的 Javascript(或类似)游戏脚本

    最近我准备用 XNA C 开发另一个游戏 上次我在 XNA C 中开发游戏时 遇到了必须向游戏中添加地图和可自定义数据的问题 每次我想添加新内容或更改游戏角色的某些值或其他内容时 我都必须重建整个游戏或其他内容 这可能需要相当长的时间 有没
  • 在哪里存储 Java 的 .properties 文件?

    The Java教程 http download oracle com javase tutorial essential environment properties htmlon using Properties 讨论如何使用 Prop
  • 在 GWT 中,在任何主机页标记上添加事件处理程序

    我想为任何标签添加 MouseOver 事件处理程序 举个例子 我想为旧版 HTML 页面中的每个锚点页面添加事件处理程序 继GWT指南 http code google com webtoolkit doc 1 6 DevGuideUse
  • 迭代 pandas 数据框的最快方法?

    如何运行数据框并仅返回满足特定条件的行 必须在之前的行和列上测试此条件 例如 1 2 3 4 1 1 1999 4 2 4 5 1 2 1999 5 2 3 3 1 3 1999 5 2 3 8 1 4 1999 6 4 2 6 1 5 1
  • 如何为 Windows toast 注册协议?

    如何注册 Windows toast 协议 样本中来自https blogs msdn microsoft com tiles and toasts 2015 07 02 adaptive and interactive toast not
  • ngmodel与Angular2中复选框的动态数组绑定

    我有一个 Angular 2 组件 其中我从数组生成复选框列表 现在我需要根据选中的复选框填充不同的数组 这应该是双向绑定 这意味着如果复选框的值已在数组中 则必须已经检查了复选框 我在 Angular 1 中使用了一个名为 checkli
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d
  • 您可以使用关键字参数而不提供默认值吗?

    我习惯于在 Python 中使用这样的函数 方法定义 def my function arg1 None arg2 default do stuff here 如果我不供应arg1 or arg2 那么默认值None or default
  • Android ScrollView fillViewport 不工作

    我有一个简单的布局 名称位于顶部 按钮位于屏幕底部 或者超出该按钮 以防我添加更多项目 所以我使用带有 LinearLayout 的 ScrollView 如下所示
  • Scrapy Spider不存储状态(持久状态)

    您好 有一个基本的蜘蛛 可以运行以获取给定域上的所有链接 我想确保它保持其状态 以便它可以从离开的位置恢复 我已按照给定的网址进行操作http doc scrapy org en latest topics jobs html http d
  • NGinx $proxy_add_x_forwarded_for 和 real_ip_header

    我在 NGinx 下有一个 web 应用程序和另一个前端负载均衡器 如下所示 x x x x IP 地址 客户端 a a a a gt LB b b b b gt NGX c c c c gt WEBAPP d d d d 这是我的 NGi
  • Typescript 函数接口重载

    我有以下代码 interface MySecondInterface a type A interface MyInterface val1 string val2 string MySecondInterface a
  • 实例化 Microsoft.Office.Interop.Excel.Application 对象时出现错误:800700c1

    实例化 Microsoft Office Interop Excel Application 以从 winforms 应用程序生成 Excel 时 出现以下错误 这之前是有效的 但突然间它停止工作了 尽管代码和 Excel 版本没有变化 我
  • 带显示块的SPAN

    和默认有什么区别 div 元素和默认值 span 元素与display block HTML 元素的有效性和语义存在差异 否则它们是相同的 div and span两者都被定义为通用容器 在 HTML 方面没有更深层次的含义 一个默认为块显
  • 使用 Crypto++ 获取 ECDSA 签名

    我必须使用 Crypto 在变量中获取 ECDSA 签名 我在启动 SignMessage 后尝试获取它 但签名为空 我怎样才能得到它 你看过 Crypto wiki 吗 上面有很多东西椭圆曲线数字签名算法 http www cryptop
  • 自定义 Visual Studio 2008 中的位置栏

    有人成功定制了 VS 2008 的 Places Bar 吗 我从 VS 2005 进行的自定义设置并没有转移到 2008 显然 并且无论我如何处理注册表 我都无法使我的自定义位置出现在 打开 对话框中 我已经阅读并应用了相关的MS KB文
  • 从 Azure 应用服务连接到 MongoDB Atlas 集群

    我在 Azure 上有一个 Web 应用程序 它连接到 Atlas cloud mongodb com 上托管的 MongoDB 集群 我想使用 Atlas 这样我就不必关心 MongoDb 配置 问题是我的集群连接超时 我必须在我的 mo
  • 使用泛型全面实现特征

    我正在通过实现矩阵数学来练习 Rust 但遇到了一些障碍 我定义了我认为与矩阵相关的特征 trait Matrix
  • Android:如何检测手机设置中的语言已更改

    我如何检测我的手机语言是否已更改 例如 Facebook 应用程序将向我们宣布 please wait we preparing your language i used myString Locale getDefault getDisp
  • CUDA 中指令重放的其他原因

    这是我从 nvprof CUDA 5 5 获得的输出 Invocations Metric Name Metric Description Min Max Avg Device Tesla K40c 0 Kernel MyKernel do

随机推荐