我在我的 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" />