在剛剛結束的谷歌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()
}
複製代碼