• (转)Activity中多fragment使用NavHostFragment导航用法


    会自动添加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" )
    }
    ————————————————
    版权声明:本文为CSDN博主「BY-91」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/ytfunnysite/article/details/108667383

  • 相关阅读:
    Go组件学习——Web框架Gin
    一次线上接口超时的排查过程
    Go组件学习——手写连接池并没有那么简单
    终于跑通分布式事务框架tcc-transaction的示例项目
    Go语言学习——如何实现一个过滤器
    Go组件学习——database/sql数据库连接池你用对了吗
    我们这一年 | 诗人呀
    NodeMan架构
    NodeMan介绍
    微信小程序录音实现
  • 原文地址:https://www.cnblogs.com/mwl523/p/15891709.html
Copyright © 2020-2023  润新知