首發(fā)于公眾號: DSGtalk1989
-
依賴,最新版本見版本號(https://mvnrepository.com/artifact/android.arch.navigation/navigation-fragment)
implementation "android.arch.navigation:navigation-fragment:1.0.0-rc02" implementation "android.arch.navigation:navigation-ui:1.0.0-rc02"補個小知識:大家對alpha,beta,rc,release以及更多你可能看到的版本后綴的解釋如下
α(Alpha)版:內測版,內部交流或者專業(yè)測試人員測試用。Bug較多,普通用戶最好不要安裝。
β(Beta)版:公測版,專業(yè)愛好者大規(guī)模測試用,存在一些缺陷,該版本也不適合一般用戶安裝。
γ(Gamma)版:相當成熟的測試版,與即將發(fā)行的正式版相差無幾。
RC版:是 Release Candidate 的縮寫,意思是發(fā)布倒計時,候選版本,處于Gamma階段,該版本已經完成全部功能并清除大部分的BUG。到了這個階段只會除BUG,不會對軟件做任何大的更改。從Alpha到Beta再到Gamma是改進的先后關系,但RC1、RC2往往是取舍關系。
Final:正式版。
右鍵
res下面new一個directory,命名為navigation。在
navigation右鍵new就會出現(xiàn)navigation resource file取名為nav_demo-
創(chuàng)建幾個你需要做切換的
fragment,DemoFragment1,DemoFragment2,DemoFragment3,放入nav_demo<?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/page1Fragment">
<fragment
android:id="@+id/page1Fragment"
android:name="org.salamanca.kotlinframe.DemoFragment1"
android:label="fragment_page1"
tools:layout="@layout/fragment_demo1">
<action
android:id="@+id/action_page2"
app:destination="@id/page2Fragment" />
</fragment>
<fragment
android:id="@+id/page2Fragment"
android:name="org.salamanca.kotlinframe.DemoFragment2"
android:label="fragment_page2"
tools:layout="@layout/fragment_demo2">
<action
android:id="@+id/action_page1"
app:popUpTo="@id/page1Fragment" />
<action
android:id="@+id/action_page3"
app:destination="@id/page3Fragment" />
</fragment>
<fragment
android:id="@+id/page3Fragment"
android:name="org.salamanca.kotlinframe.DemoFragment2"
android:label="fragment_page3"
tools:layout="@layout/fragment_demo3">
<action
android:id="@+id/action_page2"
app:popUpTo="@id/page2Fragment" />
</fragment>
</navigation>
```
`popUpTo`表示返回到,`destination`表示去到。我們在`action`還可以添加其他的諸如
```js
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
```
等的界面切換屬性。
-
在
activity布局中加入NavHostFragment這個導航界面容器<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_demo" />defaultNavHost表示是否攔截back點擊事件,需要配合第6步一起實現(xiàn)。
-
委托
back按鈕的點擊事件給到導航容器,即導航容器中一旦有fragment棧切換,就會返回到上一個fragment。具體視情況而定,比如一般首頁的fragment切換就不需要進行back委托override fun onSupportNavigateUp() = findNavController(this, R.id.my_nav_host_fragment).navigateUp() -
我們在每一個fragment中去設置跳轉到哪個fragment以及如何返回
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_demo2, container, false) tvTest.setOnClickListener { Navigation.findNavController(it).navigate(R.id.action_page3) } return view }findNavController會通過傳入的View往上一層一層的進行遍歷,直到找到NavController為止。所以這里傳入it或者view都可以。 -
跳轉的三種方式
-
navigate走destination的action,就是直接跳轉界面過去 -
navigate走popupup的action就是自己出棧并且跳轉到相應的界面 -
navigateUp就是直接自己出棧
-