- Navigation的主要元素
Navigation Graph:XML資源文件,包含應用程序的所有頁面,以及頁面的關系,
NavHostFragment:其他fragment的容器,Navigation Graph中的fragment正是通過NavHostFragment進行展示的
NavController:完成Navigation Graph中具體頁面的切換工作
總結:使用NavController對象切換fragment,Navigation Graph是明確去那個fragment,NavController把想去的fragment展示在NavHostFragment中
具體使用:
首先,添加依賴:
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
第一步:建立Navigation Graph的資源文件nav_graph
截屏2023-03-09 11.23.25.png
這個操作很簡單可以直接點擊想去的fragment
截屏2023-03-09 11.25.37.png
<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"
android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="com.app.servicedemo.FirstFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
</fragment>
<fragment
android:id="@+id/SecondFragment"
android:name="com.app.servicedemo.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second">
<action
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
</fragment>
</navigation>
action和destination完成頁面導航
startDestination:表示開始的fragment
第二步:在activity中創(chuàng)建NavHostFragment文件
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment_content_main"
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_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
app:defaultNavHost="true"則系統(tǒng)會自定處理fragment的返回鍵
app:navGraph="@navigation/nav_graph" 設置該fragment的導航
第三步:使用NavController完成導航,通過代碼實現(xiàn)具體的跳轉
在fragment中添加一個button
//方法一:
binding.buttonFirst.setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
}
//方法二:
binding.buttonFirst.setOnClickListener {
Navigation.createNavigateOnClickListener(R.id.action_SecondFragment_to_FirstFragment)
}
第四步:添加動畫:點擊箭頭直接添加動畫
截屏2023-03-09 11.41.17.png
第五步:傳遞參數(shù)
fragmentFirst的代碼(傳參數(shù)):
val bundle=Bundle()
bundle.putString("name","張三")
bundle.putInt("age",20)
binding.buttonFirst.setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment,bundle)
}
fragmentSecond獲取參數(shù)
val bundle=arguments
if (bundle!=null){
val name=bundle.getString("name")
val age=bundle.getInt("age")
binding.textviewSecond.text="name:$name,age:$age"
}