Android JetPack之Navigation(四)

在Navigation中,还新增了一种Fragment切换的方式,就是利用<action></action>进行切换。以下是action的简单使用

以下为action的属性
<action android:id="@+id/page2action"
            app:popUpToInclusive="true" 
            app:launchSingleTop="true"
            app:popUpTo="@id/mainPage1Fragment"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"
            app:destination="@id/mainPage3Fragment"/>
  • android:id="@+id/page2action" action的ID,属于必填项
  • app:popUpToInclusive="true" 是否应从后堆栈中弹出设置的目标。这个需要和app:popUpTo配合使用,效果和代码中的findNavController().popBackStack(R.id.mainPage1Fragment,false)效果一致
  • app:launchSingleTop="true" 此导航操作是否应作为单顶启动(即,后堆栈的顶上最多有一个给定目标的副本
  • app:popUpTo="@id/mainPage1Fragment"弹出的目的地,可以指定返回到哪个页面 ,这里写action的Id
  • app:enterAnim="@anim/nav_default_enter_anim" 进入动画
  • app:exitAnim="@anim/nav_default_exit_anim"退出动画
  • app:popEnterAnim="@anim/nav_default_pop_enter_anim" 按返回键或者弹出下个页面使当前页面显示的进入动画
  • app:popExitAnim="@anim/nav_default_pop_exit_anim"按返回键或者弹出当前页面的退出动画
  • app:destination="@id/mainPage3Fragment"跳转到目的地页的路径,属于必填项

该属性可以通过面板配置

跳转代码和之前一样:

findNavController().navigate(R.id.page1action,args.toBundle())

有两个区别:

  • <action></action>是位于当前<fragment></fragment>标签下面,也包含了跳转的路径,所以传入的是当前显示fragment下面action的id
  • findNavController().navigate(R.id.page1action,args.toBundle())中传入的是action的id,而不是fragment的id,并且只有是actionId的时候,<action></action>里面包含的属性才能生效

完整代码如下:

test_nav.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/test_nav"
    app:startDestination="@id/mainPage1Fragment">

    <fragment
        android:id="@+id/mainPage1Fragment"
        android:name="com.example.test.fragment.MainPage1Fragment"
        android:label="MainPage1Fragment"
        tools:layout="@layout/fragment_item1">
        <action android:id="@+id/page1action"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"
            app:destination="@id/mainPage2Fragment"/>
    </fragment>

    <fragment
        android:id="@+id/mainPage2Fragment"
        android:name="com.example.test.fragment.MainPage2Fragment"
        android:label="MainPage2Fragment"
        tools:layout="@layout/fragment_item2">
        <argument
            android:name="page2"
            app:argType="integer"
            android:defaultValue="10" />
        <action android:id="@+id/page2action"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"
            app:destination="@id/mainPage3Fragment"/>
    </fragment>

    <fragment
        android:id="@+id/mainPage3Fragment"
        android:name="com.example.test.fragment.MainPage3Fragment"
        android:label="MainPage3Fragment"
        tools:layout="@layout/fragment_item3">
        <argument
            android:name="page3"
            app:argType="string"
            android:defaultValue="默认值" />
    </fragment>
</navigation>
MainPage1Fragment.kt

class MainPage1Fragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_item1, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        go.setOnClickListener {
            val args = MainPage2FragmentArgs(1314)
            findNavController().navigate(R.id.page1action,args.toBundle())
        }
    }
}

MainPage2Fragment.kt

class MainPage2Fragment : Fragment() {
    private val args: MainPage2FragmentArgs by navArgs()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_item2, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        content.text = "获取的参数:${args.page2}"
        val args = MainPage3FragmentArgs("哈哈")
        go.setOnClickListener {
            findNavController().navigate(R.id.page2action,args.toBundle())
        }
    }
}

写到现在这里,发现了一个问题。Navigation进去Fragment切换的时候,无法保存数据,即使返回的时候也不会保存上次的数据。这个问题,下篇解决。

发布了132 篇原创文章 · 获赞 29 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/Mr_Tony/article/details/103345905