如何在子片段中实现导航控制器?

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

我在activity_main中定义了一个导航控制器片段,该片段的高度和宽度与父对象的高度匹配。因此,当我在片段之间导航时,它将替换整个屏幕。现在说我导航到一个加载物料卡的片段。我希望该物料卡具有自己的导航控制器,该控制器占用物料卡的高度和宽度。因此,当我使用其导航控制器时,它将仅替换物料卡视图中的片段,而不会更改父级视图。

是否可以使用两个导航主机片段?因此,我可以在activity_main内部定义一个,然后在子片段内部定义另一个。

activity_main:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/barrier"
            app:navGraph="@navigation/base_nav_graph" />

子片段:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="600dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

    <fragment
        android:id="@+id/nav_child_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:navGraph="@navigation/child_nav_graph" />
android kotlin android-navigation android-navigation-graph
1个回答
0
投票

请参阅高级示例https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample

[BottomNavigationView项目使用了多个导航控制器

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