使用导航组件 Android 导航到另一个图形的特定片段

时间:2021-07-17 12:35:06

标签: android kotlin navigation

我想从一个图导航到图二的特定片段。

我知道我可以导航到两个写作片段:

findNavController().navigate(FragmentOneDirections.goToTwo())

我想从片段一导航到片段三或片段四。

有没有办法直接导航?

<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/graph_one"
    app:startDestination="@id/fragment_one">

    <include app:graph="@navigation/graph_two" />

    <fragment
        android:id="@+id/fragment_one"
        android:name="com.example.ui.fragment.FragmentOne"
        android:label="FragmentOne"
        tools:layout="@layout/fragment_one">

        <action
            android:id="@+id/go_to_two"
            app:destination="@id/graph_two"
            app:enterAnim="@anim/nav_default_pop_enter_anim"
            app:exitAnim="@anim/nav_default_pop_exit_anim"/>

        </fragment>

</navigation>

<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/graph_two"
    app:startDestination="@id/fragment_two">

    <fragment
        android:id="@+id/fragment_two"
        android:name="com.example.ui.fragment.FragmentTwo"
        android:label="FragmentTwo"
        tools:layout="@layout/fragment_two">

    <fragment
        android:id="@+id/fragment_three"
        android:name="com.example.ui.fragment.FragmentThree"
        android:label="FragmentThree"
        tools:layout="@layout/fragment_three">

    <fragment
        android:id="@+id/fragment_four"
        android:name="com.example.ui.fragment.FragmentFour"
        android:label="FragmentFour"
        tools:layout="@layout/fragment_four">

    </fragment>

</navigation>

1 个答案:

答案 0 :(得分:-1)

同一个fragment可以添加到不同的graph中,使用不同的导航动作

因此您可以将 fragment_threefragment_four 添加到 graph_one 中,然后定义将您带到相应片段的操作。所以你的 graph_one 看起来如下

<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/graph_one"
    app:startDestination="@id/fragment_one">

    <include app:graph="@navigation/graph_two" />

    <fragment
        android:id="@+id/fragment_one"
        android:name="com.example.ui.fragment.FragmentOne"
        android:label="FragmentOne"
        tools:layout="@layout/fragment_one">

        <action
            android:id="@+id/go_to_two"
            app:destination="@id/graph_two"
            app:enterAnim="@anim/nav_default_pop_enter_anim"
            app:exitAnim="@anim/nav_default_pop_exit_anim"/>

       <!-- ACTION TO MOVE TO FRAGMENT THREE -->
       <action
            android:id="@+id/go_to_fragment_three"
            app:destination="@id/fragment_three"
            app:enterAnim="@anim/nav_default_pop_enter_anim"
            app:exitAnim="@anim/nav_default_pop_exit_anim"/>

        </fragment>

       <fragment
          android:id="@+id/fragment_three"
          android:name="com.example.ui.fragment.FragmentThree"
          android:label="FragmentThree"
          tools:layout="@layout/fragment_three">

       <fragment
          android:id="@+id/fragment_four"
          android:name="com.example.ui.fragment.FragmentFour"
          android:label="FragmentFour"
          tools:layout="@layout/fragment_four">

</navigation>

现在您可以导航到fragment_three

findNavController().navigate(R.id.go_to_fragment_three)
相关问题