## Jetpack-Navigation(kotlin)(一)
學(xué)習(xí)筆記巧号,案例來自于:
https://www.bilibili.com/video/BV1w4411t7UQ/?p=17
一嫩与、基本概念---該部分直接引用谷歌官網(wǎng)介紹
1.基本組成
- 導(dǎo)航圖:在一個集中位置包含所有導(dǎo)航相關(guān)信息的 XML 資源。這包括應(yīng)用內(nèi)所有單個內(nèi)容區(qū)域(稱為目標)以及用戶可以通過應(yīng)用獲取的可能路徑饥努。
-
NavHost
:顯示導(dǎo)航圖中目標的空白容器。導(dǎo)航組件包含一個默認NavHost
實現(xiàn) (NavHostFragment
)盈简,可顯示 Fragment 目標常遂。 -
NavController
:在NavHost
中管理應(yīng)用導(dǎo)航的對象。當用戶在整個應(yīng)用中移動時侥锦,NavController
會安排NavHost
中目標內(nèi)容的交換进栽。
在應(yīng)用中導(dǎo)航時,您告訴 NavController
恭垦,您想沿導(dǎo)航圖中的特定路徑導(dǎo)航至特定目標快毛,或直接導(dǎo)航至特定目標。NavController
便會在 NavHost
中顯示相應(yīng)目標番挺。
2.優(yōu)勢
- 處理 Fragment 事務(wù)唠帝。
- 默認情況下,正確處理往返操作玄柏。
- 為動畫和轉(zhuǎn)換提供標準化資源襟衰。
- 實現(xiàn)和處理深層鏈接。
- 包括導(dǎo)航界面模式(例如抽屜式導(dǎo)航欄和底部導(dǎo)航)粪摘,用戶只需完成極少的額外工作瀑晒。
- Safe Args - 可在目標之間導(dǎo)航和傳遞數(shù)據(jù)時提供類型安全的 Gradle 插件绍坝。
-
ViewModel
支持 - 您可以將ViewModel
的范圍限定為導(dǎo)航圖,以在圖表的目標之間共享與界面相關(guān)的數(shù)據(jù)苔悦。 - 使用 Android Studio 的 Navigation Editor 來查看和編輯導(dǎo)航圖轩褐。
注意:需要 Android Studio 3.3 或更高版本。
3.具體使用案例
1.新建兩個fragment
2.新建一個navigation資源文件玖详,并且打開切換至“design"視圖
4.通過可視化設(shè)置導(dǎo)航
設(shè)置navigation資源文件
設(shè)置activity_main:添加NavHostFragment控件把介,設(shè)置其屬性navGraph=你的navigation資源文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/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/navigation_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
5.代碼編寫
MainActivity
package com.example.navigationdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//注意:該布局文件id---是NavHostFragment的id
val navController:NavController=Navigation.findNavController(this,R.id.fragment)
//顯示左上角返回鍵
NavigationUI.setupActionBarWithNavController(this,navController)
}
//設(shè)置左上角返回鍵的點擊
override fun onSupportNavigateUp(): Boolean {
return Navigation.findNavController(this,R.id.fragment)
.navigateUp()
}
}
DetailFragment
package com.example.navigationdemo
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_detail.*
/**
* A simple [Fragment] subclass.
* Use the [DetailFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class DetailFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
//按鍵點擊事件
button2.setOnClickListener(View.OnClickListener {
Navigation.findNavController(it)
.navigate(R.id.action_detailFragment_to_homeFragment) //該id是Android studio 生成的無需手動
})
}
}