在刚刚结束的谷歌IO大会上,谷歌推出了Android Jetpack架构组件;正如官网所说,Android Jetpack 是一套组件、工具和指导,能够帮助您构建出色的 Android 应用。Android Jetpack 组件将现有的支持库与架构组件联系起来,并将它们分红四个类别:Architecture、Foundation、Behavior 以及 UI。他可以让开发者创造出更加出色的高质量应用。java
Navigation导航编辑器做为Android Jetpack和 AndroidX 依赖库的一部分其目标旨在简化Android开发中导航的实现。Navigation能够帮助咱们很好的处理Activity和fragment之间经过FragmentTransaction交互的复杂性。另外Navigation也能够很好的处理页面的转场效果。固然熟悉IOS开发的同窗确定看到这确定会以为这不就是StoryBoard么。关于Navigation的介绍,谷歌官方介绍的也不是很详细(英语太差~~~),那么咱们就动手实现个吧!!!android
buildscript {
...
repositories {
google()
}
dependencies {
...
classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha01'
}
}
复制代码
apply plugin: 'androidx.navigation.safeargs'
复制代码
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha01'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha01'
复制代码
右键res资源文件夹 : New -> Android resource file -> 输入xml文件名称并选择Resource type为Navigation -> OKgit
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android">
</navigation>
复制代码
因为Navigation 中须要将fragment视图和activity绑定,那么接下来改造吧:github
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:navGraph="@navigation/main_navigation" />
</android.support.constraint.ConstraintLayout>
复制代码
activity中fragment默认为NavHostFragment,NavHostFragment经过navGraph与navigation导航编辑器进行关联。 app:defaultNavHost="true"
可让NavHostFragment处理系统的返回事件bash
<?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"
app:startDestination="@+id/main_fragment">
<fragment
android:name="com.wangjun.app.jetpacktodolist.ui.main.MainFragment"
android:id= "@+id/main_fragment"
android:label="@string/main_fragment_title"
tools:layout="@layout/main_fragment">
</fragment>
<activity
android:id="@+id/settings_activity"
android:name="com.wangjun.app.jetpacktodolist.ui.SettingActivity"
android:label="@string/activity_settings"
tools:layout="@layout/setting_activity"
/>
</navigation>
复制代码
咱们看到navigation标签声明了一个app:startDestination="@+id/main_fragment"
属性,他是导航器默认加载的视图架构
如今咱们看到咱们的导航编辑器又两个视图,main_fragment和 settings_activity,咱们如今须要从main_fragment中跳转到 settings_activity的话,能够给main_fragment添加action标签来完成跳转app
<?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"
app:startDestination="@+id/main_fragment">
<fragment
android:name="com.wangjun.app.jetpacktodolist.ui.main.MainFragment"
android:id= "@+id/main_fragment"
android:label="@string/main_fragment_title"
tools:layout="@layout/main_fragment">
<!--跳转到SettingActivity-->
<action
android:id="@+id/action_main_fragment_to_settings_activity"
app:destination="@id/settings_activity" />
</fragment>
<activity
android:id="@+id/settings_activity"
android:name="com.wangjun.app.jetpacktodolist.ui.SettingActivity"
android:label="@string/activity_settings"
tools:layout="@layout/setting_activity"
/>
</navigation>
复制代码
action标签中的app:destination
就是咱们要加载导航的视图编辑器
固然咱们也能够在导航编辑器中经过拖动来完成ide
接下来咱们在MainFragment中添加一个按钮来完成跳转SettingActivity工具
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MainFragment">
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</android.support.constraint.ConstraintLayout>
复制代码
class MainFragment : Fragment() {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.main_fragment, container, false)
view.findViewById<AppCompatButton>(R.id.btn_setting).setOnClickListener {
Navigation.findNavController(view).navigate(R.id.action_main_fragment_to_settings_activity)
}
return view
}
}
复制代码
咱们看到经过Navigation.findNavController(view)
获得一个NavController,经过NavController.navigate(R.id.xxxx)
会对应到当前View的某个Action,这样咱们就能够跳转了。是否是很简单~~~
添加转场动画 添加转场动画也很简单,咱们只须要在action添加以下属性
<fragment
android:name="com.wangjun.app.jetpacktodolist.ui.main.MainFragment"
android:id= "@+id/main_fragment"
android:label="@string/main_fragment_title"
tools:layout="@layout/main_fragment">
<action
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
android:id="@+id/action_main_fragment_to_settings_activity"
app:destination="@id/settings_activity" />
</fragment>
复制代码
和之前同样咱们能够经过bundle传递数据
代码中手写
<fragment
android:id="@+id/main2_fragment"
android:name="com.wangjun.app.jetpacktodolist.ui.main.Main2Fragment"
android:label="@string/main2_fragment_title"
tools:layout="@layout/main2_fragment">
<argument android:name="testArg"
app:type="string"
android:defaultValue="Hello Leon"
/>
<argument
android:name="testArg2"
android:defaultValue="大王叫我来巡山"
app:type="string" />
</fragment>
复制代码
导航编辑器添加
MainFragment传参到 Main2Fragment
class MainFragment : Fragment() {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.main_fragment, container, false)
view.findViewById<AppCompatButton>(R.id.btn_setting).setOnClickListener {
Navigation.findNavController(view).navigate(R.id.action_main_fragment_to_settings_activity)
}
/**
* 参数跳转
*/
view.findViewById<AppCompatButton>(R.id.btn_main2).setOnClickListener {
val bundle = bundleOf("testArg" to "很高兴碰见你",
"testArg2" to "你是猴子派来的逗逼吗")
Navigation.findNavController(view).navigate(
R.id.action_main_fragment_to_main2_fragment,
bundle)
}
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
}
}
复制代码
class Main2Fragment : Fragment() {
private lateinit var testArg: String
private lateinit var testArg2: String
companion object {
fun newInstance() = Main2Fragment()
}
@SuppressLint("SetTextI18n")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.main2_fragment, container, false)
arguments?.let {
testArg = it["testArg"] as String
testArg2 = it["testArg2"] as String
}
var tvMain = view.findViewById<AppCompatTextView>(R.id.tv_main)
tvMain.text = "$testArg---$testArg2"
return view
}
}
复制代码
咱们发现传参是经过NavController 来传递的,具体源码以下:
NavController.java
/**
* Navigate to a destination from the current navigation graph. This supports both navigating
* via an {@link NavDestination#getAction(int) action} and directly navigating to a destination.
*
* @param resId an {@link NavDestination#getAction(int) action} id or a destination id to
* navigate to
* @param args arguments to pass to the destination
*/
public final void navigate(@IdRes int resId, @Nullable Bundle args) {
navigate(resId, args, null);
}
复制代码
其实很好理解的,也就是咱们之前所说的自定义URL使用Scheme方式来跳转传参
navigation.xml
<navigation>
<activity
android:id="@+id/settings_activity"
android:name="com.wangjun.app.jetpacktodolist.ui.SettingActivity"
android:label="@string/activity_settings"
tools:layout="@layout/setting_activity" >
<!--深层连接-->
<deepLink app:uri="www.leonwang.com/hello/{testArg}" />
</activity>
</navigation>
复制代码
AndroidManifest.xml
<activity
android:name=".ui.SettingActivity"
android:label="@string/activity_settings">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="https"/>
<data android:scheme="http"/>
<data android:host="www.leonwang.com"/>
<data android:pathPrefix="/hello/"/>
</intent-filter>
</activity>
复制代码
根据官方文档说明,为了保证导航的正确使用,咱们须要在目标的Activity中重写onSupportNavigateUp,以确保导航器可以正确的回退栈。
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp()
}
复制代码