navigation的action跳转,切换动画和传参

1,action

navigation_main.xml中设置action

<action android:id="@+id/action_dashBoardSampleFragment_to_notificationSampleFragment" app:destination="@id/notificationSampleFragment"/>

action设置好了之后,我们可以执行下面代码进行跳转:

findNavController().navigate(R.id.action_homeSampleFragment_to_dashBoardSampleFragment_action)
 

2,NavOptions切换动画

NavOptions是一个动画管理类,我们可以设置进入和回退的动画,设置的方式有两种:

a,直接在标签中设置动画

<action android:id="@+id/action_homeSampleFragment_to_dashBoardSampleFragment_action"
                app:destination="@id/dashBoardSampleFragment"
                app:enterAnim="@anim/slide_in_right"
                app:exitAnim="@anim/slide_out_left"
                app:popEnterAnim="@anim/slide_in_left"
                app:popExitAnim="@anim/slide_out_right"/>
b,通过NavOptions设置动画

val options = navOptions {
            anim {
                enter = R.anim.slide_in_right
                exit = R.anim.slide_out_left
                popEnter = R.anim.slide_in_left
                popExit = R.anim.slide_out_right
            }
        }
        view.findViewById<Button>(R.id.navigate_destination_button)?.setOnClickListener {
            findNavController().navigate(R.id.flow_step_one_dest, null, options)
        }
3,参数传递

<argument
         android:name="deep_args"
         app:argType=""
         android:defaultValue=""
         app:nullable=""/>

  • name是我们传参的key
  • argType是参数类型
  • defaultValue默认值
  • nullable 是否可空
  • **注意:**当然type类型也支持我们自定的实体类,但是需要你填写类的全路径,同时你要保证实体类实现了序列化

val args = Bundle()
args.putString("link","1")
args.putString("title","1")
it.findNavController().navigate(R.id.webFragment, args)
 

ConfirmationFragmentArgs

override fun onClick(v: View) {
   val amountTv: EditText = view!!.findViewById(R.id.editTextAmount)
   val amount = amountTv.text.toString().toInt()
   val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
   v.findNavController().navigate(action)
}
val args: ConfirmationFragmentArgs by navArgs()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val tv: TextView = view.findViewById(R.id.textViewAmount)
    val amount = args.amount
    tv.text = amount.toString()
}

猜你喜欢

转载自blog.csdn.net/whb008/article/details/113973169
今日推荐