天天看點

【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數

一、Navigation

1.建立NavigationGraph

【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數
【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數

2.添加NavHostFragment

在Activity布局檔案的根節點下添加NavHostFragment

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

3.建立兩個Fragment

建立MainFragment及SecondFragment,在MainFragment中建立按鈕,用于導航到SecondFragment

4.建立destination

在建立的nav_graph.xml檔案中,點選加号,選擇fragment_main和fragment_second,添加到可視面闆中

【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數

5.建立action

将滑鼠放到mainfragment右側圓點上,并拖拽到右側secondFragment,建立action

【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數
【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數

6.使用NavController完成導航

在MainFragment中onCreateView()方法中添加如下代碼實作導航跳轉,這裡有兩種方式實作導航

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_main, container, false)

        //方式一
        view.findViewById<Button>(R.id.btntoSecondFragment)
            .setOnClickListener {
                Navigation.findNavController(view)
                    .navigate(R.id.action_mainFragment_to_secondFragment)
            }

        //方式二
        view.findViewById<Button>(R.id.btntoSecondFragment)
            .setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_mainFragment_to_secondFragment))

        return view
    }
           

7.添加動畫

建立動畫檔案,并在nav_graph.xml中選中action箭頭,在右側屬性中添加動畫

【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數
【Jetpack系列】Navigation簡單使用(Kotlin)一、Navigation二、使用safe args插件傳遞參數

二、使用safe args插件傳遞參數

1.配置gradle

在Project的build.gradle檔案中dependencied層級,添加classpath

classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.3'
           

在Module的build.gradle中添加插件

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs'
}
           

2.添加argument

在nav_graph.xml檔案中,mainfragment中添加<argument>标簽

<argument
            android:name="user_name"
            app:argType="string"
            android:defaultValue="unknown" />
        <argument
            android:name="age"
            app:argType="integer"
            android:defaultValue="0" />
           

3.在MainFragment中傳參

val bundle=MainFragmentArgs.Builder()
        .setUserName("zy")
        .setAge(27)
        .build()
        .toBundle()
    Navigation.findNavController(view)
    .navigate(R.id.action_mainFragment_to_secondFragment,bundle)
           

4.在SecondFragment中接收參數

val userName=MainFragmentArgs.fromBundle(requireArguments()).userName
        val age=MainFragmentArgs.fromBundle(requireArguments()).age
        view.findViewById<TextView>(R.id.tv).text="userName: $userName \nage: $age"
           

源碼已上傳Gtihub

https://github.com/ZYALLZ/AndroidJetpack