Android 导航组件将 2 个嵌套片段加载到父片段中

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

我有一个比较器屏幕,它是一个分为 2 个子屏幕的片段。在使用导航组件之前,我可以轻松地:

 private void initializeFragments() {

        FoodComparatorFragment comparatorFragment1 = new FoodComparatorFragment();
        FoodComparatorFragment comparatorFragment2 = new FoodComparatorFragment();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.comparator_fragment_1, comparatorFragment1);
        transaction.add(R.id.comparator_fragment_2, comparatorFragment2);

        transaction.commit();
    }

但是,我不知道应该如何使用导航组件执行此操作而不显式使用

FragmentManager

android android-jetpack android-architecture-components android-architecture-navigation
1个回答
1
投票

默认情况下,导航组件有一个

NavHostFragment
由一个活动/多个片段模型的主活动托管。对于许多活动/片段模型,每个活动都有自己的
NavHostFragment/NavGraph

在这里,我们将考虑一个活动,多个片段模型,其中这些片段之一是比较器屏幕(父片段),它被分成 2 个子屏幕/片段。

但是

NavHostFragment
一次只能承载一个片段;所以这里的想法是在比较器/父片段中有两个
NavHostFragments


考虑下面的例子:

  • 我们有一个
    MainActivity
    ,其中包含两个片段(
    HomeFragment
    ParentFragment
    )。
  • ParentFragment
    是具有两个
    NavHostFragments
    的比较器,每个可以承载一个片段;上面的是
    TopFragment
    ,下面的是
    BottomFragment
  • 有一个动作可以从
    HomeFragment
    ParentFragment
  • int
    参数发送到每个子屏幕,即顶部/底部片段,通过将参数以及从
    HomeFragment
    ParentFragment
    的操作发送到子屏幕来完成;它们将显示在屏幕上的
    TextViews
    上。
MainActivity (1 NavHostFragments/NavGraph) ->> nav_host_fragment_activity_main
    |
    |---- HomeFragment
    |
    |---- ParentFragment (2 NavHostFragments/NavGraphs) ->> top_nav_host_fragment & bottom_nav_host_fragment
    |       |
    |       |---- TopFragment
    |       |
    |       |---- BottomFragment

现在我们有 3 个导航图:

主要(mobile_navigation.xml):

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example....HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_homeFragment_to_parentFragment"
            app:destination="@id/navigation_parent"
            app:popUpTo="@id/navigation_home"
            app:popUpToInclusive="false" />

    </fragment>

    <fragment
        android:id="@+id/navigation_parent"
        android:name="com.example....ParentFragment"
        android:label="@string/title_parent"
        tools:layout="@layout/fragment_parent">

        <argument
            android:name="top_arg"
            android:defaultValue="-1"
            app:argType="integer" />

        <argument
            android:name="bottom_arg"
            android:defaultValue="-1"
            app:argType="integer" />

    </fragment>

</navigation>

ParentFragment
中,另外两个
NavGraphs
,每个代表一个子屏幕:

对于第一个子屏幕(top_navigation.xml):

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/top_navigation"
    app:startDestination="@+id/top_fragment">

    <fragment
        android:id="@+id/top_fragment"
        android:name="com.example.....TopFragment"
        tools:layout="@layout/fragment_top">

        <argument
            android:name="arg"
            android:defaultValue="-1"
            app:argType="integer" />

    </fragment>

</navigation>

对于第二个子屏幕(bottom_navigation.xml):

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_navigation"
    app:startDestination="@+id/bottom_fragment">

    <fragment
        android:id="@+id/bottom_fragment"
        android:name="com.example.....BottomFragment"
        tools:layout="@layout/fragment_bottom">

        <argument
            android:name="arg"
            android:defaultValue="-1"
            app:argType="integer" />

    </fragment>

</navigation>

布局:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment_parent.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.fragments.HomeFragment">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/top_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/teal_200"
        app:layout_constraintBottom_toTopOf="@id/bottom_nav_host_fragment"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/bottom_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_nav_host_fragment" />


</androidx.constraintlayout.widget.ConstraintLayout>

fragment_top/bottom.xml 很简单

fragment_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.fragments.ParentFragment">

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:text="top fragment"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

顶部和底部片段的

NavGraph
将以编程方式设置,以便允许将参数从主
NavGraph
传递到子屏幕的
NavGraphs

现在在

HomeFragment
中,有一个按钮可以转到
ParentFragment
,其操作在
NavGraph
中定义,该操作发送两个子屏幕所需的参数:

findNavController().navigate(
    HomeFragmentDirections.actionHomeFragmentToParentFragment()
    .setTopArg(100)
    .setBottomArg(200))

然后在

ParentFragment
处使用
SafeArgs
接收参数:

// get fragment arguments through SafeArgs
val args = ParentFragmentArgs.fromBundle(requireArguments()) 
val bottomBundle = Bundle()
bottomBundle.putInt("arg", args.bottomArg)
val topBundle = Bundle()
topBundle.putInt("arg", args.topArg)
// setting the navGraph of the top/bottom NavHostFragments, 
// and pass the argument along as a bundle.
val bottomNavHostFragment =
    childFragmentManager.findFragmentById(R.id.bottom_nav_host_fragment)
            as NavHostFragment
bottomNavHostFragment.navController.setGraph(R.navigation.bottom_navigation, bottomBundle)

val topNavHostFragment = childFragmentManager.findFragmentById(R.id.top_nav_host_fragment)
        as NavHostFragment
topNavHostFragment.navController.setGraph(R.navigation.top_navigation, topBundle)

再次在目标顶部/底部子屏幕上,使用

safeArgs
获取参数,并将其设置为
TextView
:

// TopFragment
textView.text = TopFragmentArgs.fromBundle(
    requireArguments()
).arg.toString()

// BottomFragment
textView.text = BottomFragmentArgs.fromBundle(
    requireArguments()
).arg.toString()

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