初尝Android Jetpack 之Navigation

2018-05-31  本文已影响0人  tao72909

什么是 Navigation

  • The Navigation Architecture Component simplifies the implementation of navigation in an Android app

Navigation能做什么

利用Navigation组件对 Fragment 的原生支持,您可以获得架构组件的所有好处(例如生命周期和 ViewModel),同时让此组件为您处理 FragmentTransaction 的复杂性。此外,Navigation组件还可以让您声明我们为您处理的转场。它可以自动构建正确的“向上”和“返回”行为,包含对深层链接的完整支持,并提供了帮助程序,用于将导航关联到合适的 UI 小部件,例如抽屉式导航栏和底部导航。

怎么使用Navigation

1.目前仅在Android Studio 3.2(目前是preview)版本以上才支持

2.添加项目依赖

新建一个项目,
(1)在project的build.gradle中,添加项目的依赖

buildscript {
    ...
    repositories {
            google()
    }
    dependencies {
            ...
            classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha01'
    }
  }

(2)在app的build.gradle中添加如下依赖

apply plugin: 'androidx.navigation.safeargs'
dependencies {
    ...
    def nav_version = "1.0.0-alpha01"

    implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-ui:$nav_version" // use -ktx for Kotlin 
    // optional - Test helpers
    androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version" // use -ktx for Kotlin
}

(3)创建Navigation

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="navigation.xxw.com.navigationdemo.FragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB" />
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="navigation.xxw.com.navigationdemo.FragmentB"
        android:label="fragment_b"
        tools:layout="@layout/fragment_b" />
</navigation>
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_main"
app:defaultNavHost="true"
Navigation.findNavController(it).navigateUp()
   <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:navGraph="@navigation/nav_main" />
     <Button
        android:id="@+id/btn_go_to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击跳转到Fragment B"
        app:layout_constraintTop_toBottomOf="@id/text" />
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        view?.findViewById<Button>(R.id.btn_go_to)?.setOnClickListener {
            Navigation.findNavController(it).navigate(R.id.action_fragmentA_to_fragmentB)
        }
    }
        var bundle = Bundle()
        bundle.putString("name","zhangsan")
        view?.findViewById<Button>(R.id.btn_go_to_bundle)?.setOnClickListener {
            Navigation.findNavController(it).navigate(R.id.action_fragmentA_to_fragmentB,bundle)
        }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var bundle = arguments
        if (bundle != null) {
            var name = bundle?.getString("name")
            view.findViewById<TextView>(R.id.textB).text = "来自Fragment A页面的参数:"+name
        }
    }
    <fragment
        android:id="@+id/fragmentB"
        android:name="navigation.xxw.com.navigationdemo.FragmentB"
        android:label="fragment_b"
        tools:layout="@layout/fragment_b" >
        <argument android:name="text" android:defaultValue="Hello" app:type="string"/>
    </fragment>

其中name类似于map中的key,defaultValue是默认值,type对应是数据类型
在Fragment A页面传值通过自动生成的FragmentBArgs来进行实现bundle

  val bundle1 = FragmentBArgs.Builder().setText("我是通过argument标签实现传值").build().toBundle()
  view?.findViewById<Button>(R.id.btn_go_to_argument)?.setOnClickListener {
     Navigation.findNavController(it).navigate(R.id.action_fragmentA_to_fragmentB,bundle1)
 }

同样在FragmentB页面中通过FragmentBArgs来获取参数

//通过arguments获取参数
        val text = FragmentBArgs.fromBundle(arguments).text
        view.findViewById<TextView>(R.id.textB).text = "arguments方式获取参数:"+text
apply plugin: 'androidx.navigation.safeargs'
    <fragment
        android:id="@+id/fragmentA"
        android:name="navigation.xxw.com.navigationdemo.FragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >

        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"/>
    </fragment>
        val options = NavOptions.Builder()
                .setEnterAnim(R.anim.slide_in_right)
                .setExitAnim(R.anim.slide_out_left)
                .setPopEnterAnim(R.anim.slide_in_left)
                .setPopExitAnim(R.anim.slide_out_right)
                .build()

        view?.findViewById<Button>(R.id.btn_go_to_anim)?.setOnClickListener {
            Navigation.findNavController(it).navigate(R.id.action_fragmentA_to_fragmentB,null,options)
        }
上一篇 下一篇

猜你喜欢

热点阅读