美文网首页
Jetpack - Nativation使用

Jetpack - Nativation使用

作者: 李小轰 | 来源:发表于2021-06-11 10:38 被阅读0次

    前段时间都忙于公司的flutter项目开发,新知识我们要探索,旧能力我们也要巩固。今天我们来说说 Android Jetpack 架构里的组件:Navigation。

    前言

    首先,我们来简单介绍一下Navigation。一个适用于单Activity应用的首选架构,用于替换 fragment 的 trasation 方案。将Fragment页面的跳转交由Navigation处理,包括转场动画以及页面传参。

    基本使用

    在app的build文件中添加依赖:

    def nav_version = "2.1.0"
    
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    

    新建MainActiviy,其布局文件 main_fragment.xml 内容对应如下:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"
        tools:context=".MainActivity">
    
        <fragment
            android:id="@+id/my_nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_graph_main"/>
    </FrameLayout>
    

    注意两个点:

    • android:name="androidx.navigation.fragment.NavHostFragment" 固定写法
    • app:navGraph="@navigation/nav_graph_main" 指向的是我们自定义的navigation 文件

    res 文件夹下新建文件夹 navigation,创建 nav_graph_main.xml (名字随意)

    • 右键 - new - Navigation Resource File
    • 将相应的Fragment页面写入布局,以及对应的跳转关系,使用action标签进行关联
    <?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_graph_main"
        app:startDestination="@id/fragment1">
    
        <fragment
            android:id="@+id/fragment1"
            android:name="com.lyf.myjetpack.ui.main.Fragment1"
            android:label="main_fragment"
            tools:layout="@layout/fragment_1" >
            <action
                android:id="@+id/action_fragment1_to_fragment2"
                app:popEnterAnim="@anim/slide_in_left"
                app:popExitAnim="@anim/slide_out_right"
                app:enterAnim="@anim/slide_in_right"
                app:exitAnim="@anim/slide_out_left"
                app:destination="@id/fragment2" />
        </fragment>
    
        <fragment
            android:id="@+id/fragment2"
            android:name="com.lyf.myjetpack.ui.main.Fragment2"
            android:label="fragment2"
            tools:layout="@layout/fragment_2" >
        </fragment>
    </navigation>
    

    如上,startDestination 标识导航的起始入口为 fragment1 , fragment1 触发 actionId 为 action_fragment1_to_fragment2 的动作后能跳转到 页面fragment2


    image.png
    NavController

    通过findNavController来获取NavController,通过controller的navigate或者navigateUp进行页面之间的路由操作。

    view.findViewById<Button>(R.id.jumpToFragment2).setOnClickListener {
                val bundle = bundleOf("rex" to "name")
                Navigation.findNavController(it).navigate(
                    R.id.action_fragment1_to_fragment2,
                    bundle,
                )
            }
    //action_fragment1_to_fragment2 即为我们定义的action id,对应跳转到页面fragment2
    

    接收传参

    val args = arguments
    val name = args?.getString("name")
    
    总结

    在NavHostFragment的onCreateView中是创建了FrameLayout,也就是说其实真正的容器是FrameLayout。在创建FragmentNavigator的时候内部使用的是replace这个API,而不是show和hide。这就会导致fragment每次生命周期都会重新执行。所以和ViewModel结合使用效果应该更好

    相关文章

      网友评论

          本文标题:Jetpack - Nativation使用

          本文链接:https://www.haomeiwen.com/subject/etbmeltx.html