我想从一个图导航到图二的特定片段。
我知道我可以导航到两个写作片段:
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>
答案 0 :(得分:-1)
因此您可以将 fragment_three
和 fragment_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)