Android Navigation Component load 2 nested Fragments into Parent Fragment

0yycz8jy  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(84)

我有一个比较器屏幕,它是一个片段,分为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的情况下使用导航组件执行此操作。

u3r8eeie

u3r8eeie1#

默认情况下,导航组件有一个NavHostFragment,由一个活动/多个片段模型的主活动托管。对于多活动/片段模型,每个活动都有自己的NavHostFragment/NavGraph
在这里,我们将考虑一个活动,许多片段模型,其中一个片段是比较器屏幕(父片段),它被分成2个子屏幕/片段。
但是NavHostFragment一次只能承载一个片段;所以这里的想法是在比较器/父片段中有两个NavHostFragments

考虑以下示例:

  • 我们有一个MainActivity,它托管两个片段(HomeFragmentParentFragment)。
  • ParentFragment是具有两个NavHostFragments的比较器,每个可以托管单个片段;上面的是TopFragment,下面的是BottomFragment
  • 存在从HomeFragment转到ParentFragment的操作
  • int参数发送到每个子屏幕,顶部/底部片段,通过将参数发送到子屏幕沿着从HomeFragmentParentFragment的操作来完成;它们将显示在屏幕上的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个NavGraph:

Main(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))

然后用SafeArgs接收ParentFragment上的参数:

// 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()

相关问题