创建navigation容器

创建存放fragment的navigation容器
在这里插入图片描述
会自动添加fragment依赖

    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'

容器nav_container,用来放fragment

<?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/nav_container"
    app:startDestination="@id/nav1Fragment">

    <fragment
        android:id="@+id/nav1Fragment"
        android:name="com.bliss.myapp.Nav1Fragment"
        android:label="fragment_nav1"
        tools:layout="@layout/fragment_nav1" >
        <action
            android:id="@+id/action_nav1Fragment_to_nav2Fragment2"
            app:destination="@id/nav2Fragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"/>
    </fragment>
    <fragment
        android:id="@+id/nav2Fragment"
        android:name="com.bliss.myapp.Nav2Fragment"
        android:label="fragment_nav2"
        tools:layout="@layout/fragment_nav2" />
</navigation>

activity中引用fragment容器

引用容器nav_container

 <fragment
        android:id="@+id/nav_host_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_container"
        android:name="androidx.navigation.fragment.NavHostFragment"/>

  • app:defaultNavHost=“true” 该Fragment会自动处理系统返回键,即,当用户按下手机的返回按钮时,系统能自动将当前的Fragment推出。
  • app:navGraph 设置该Fragment对应的导航图 。

容器中在design视图下创建fragment

在这里插入图片描述

fragment间跳转

        view.findViewById<Button>(R.id.btnToSecondFragment).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_nav1Fragment_to_nav2Fragment2))

fragment跳转动画

<fragment
        android:id="@+id/nav1Fragment"
        android:name="com.bliss.myapp.Nav1Fragment"
        android:label="fragment_nav1"
        tools:layout="@layout/fragment_nav1" >
        <action
            android:id="@+id/action_nav1Fragment_to_nav2Fragment2"
            app:destination="@id/nav2Fragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"/>
    </fragment>

转场动画

  • app:popEnterAnim 点击返回键action所在元素入场的动画,是Fargment1入场的动画
  • app:popExitAnim 点击返回 ,Fragment2离场的动画
  • app:enterAnim action目的地进入的动画,是Fragment2入场的动画
  • app:exitAnim 这个action所在元素离开的动画,是Fargment1离场的动画

Bundle传参

class Nav1Fragment : Fragment(),View.OnClickListener {
    var parms = Bundle()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflater.inflate(R.layout.fragment_nav1, container, false)
        parms.putString("name","秦时明月")
//        view.findViewById<Button>(R.id.btnToSecondFragment).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_nav1Fragment_to_nav2Fragment2,parms))
        view.findViewById<Button>(R.id.btnToSecondFragment).setOnClickListener(this)
        return view
    }

    override fun onClick(view: View?) {
        when(view?.id){
            R.id.btnToSecondFragment -> Navigation.findNavController(view).navigate(R.id.action_nav1Fragment_to_nav2Fragment2,parms)
        }
    }
}
class Nav2Fragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        var param1 = arguments
        var view  = inflater.inflate(R.layout.fragment_nav2, container, false)
        Log.e("TAG", "onCreateView: param == $param1" )
        if (param1!=null){
            var name  = param1?.get("name")
            Log.e("TAG", "onCreateView: name == $name" )
        }
        return view
    }

}

safe args传参

APP根目录下的build.gradle添加依赖

 def nav_version = "2.3.0-alpha01"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

在app的build.gradle文件中添加

apply plugin: 'androidx.navigation.safeargs'
  • 在导航图中添加标签。我们可以直接在Text中编写XML代码,也可以通过Design面板添加
 <fragment
        android:id="@+id/nav1Fragment"
        android:name="com.bliss.myapp.Nav1Fragment"
        android:label="fragment_nav1"
        tools:layout="@layout/fragment_nav1" >
        <action
            android:id="@+id/action_nav1Fragment_to_nav2Fragment2"
            app:destination="@id/nav2Fragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"/>
        <argument
            android:name="age"
            app:argType="integer"
            android:defaultValue="0">
        </argument>

    </fragment>
  • rebuild 项目,会生成对应的参数相关的代码
    在这里插入图片描述
  • 利用生成的代码传递参数
   var safeArgs = Nav1FragmentArgs.Builder().setAge(100).build().toBundle() //safeargs传参
        when(view?.id){
            R.id.btnToSecondFragment -> Navigation.findNavController(view).navigate(R.id.action_nav1Fragment_to_nav2Fragment2,safeArgs)
//            R.id.btnToSecondFragment -> Navigation.findNavController(view).navigate(R.id.action_nav1Fragment_to_nav2Fragment2,parms)
        }
  • 其他fragment接受参数
        if (arguments != null){
            var safeArgs = Nav1FragmentArgs.fromBundle(requireArguments()).age
            Log.e("TAG", "onCreateView: safeArgs = =$safeArgs" )
        }
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐