Android-Jetpack-Navigation組件使用示例
Navigation的優(yōu)勢
可能有部分Android開發(fā)的小伙伴見過單Activity多Fragment的App,使用起來非常的流暢或者說非常的絲滑。自己想要嘗試這種開發(fā)模式的時候,又會發(fā)現(xiàn)Fragment的管理會比較麻煩?,F(xiàn)在不用怕了,Android SDK為了我們提供了Navigation來實現(xiàn)這種開發(fā)模式。希望這篇文章對小伙伴們有所啟發(fā)。
先來說說使用Navigation的優(yōu)勢:
- 可以可視化頁面的導(dǎo)航,可以幫我們快速理清頁面之間的關(guān)系;
- 通過
destination和action完成頁面之間的導(dǎo)航; - 方便我們添加頁面的切換動畫;
- 頁面之間類型安全的參數(shù)傳遞;
- 通過
NavigationUI類,對菜單、底部導(dǎo)航、抽屜菜單導(dǎo)航進行統(tǒng)一的管理; - 支持深層鏈接
DeepLink(深層鏈接)。
project的Navigation依賴設(shè)置
dependencies {
def nav_version = "2.4.2"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
module的Navigation依賴設(shè)置
plugins {
id 'androidx.navigation.safeargs'
}
Java版本:
implementation "androidx.navigation:navigation-fragment:2.4.1" implementation "androidx.navigation:navigation-ui:2.4.1"
Kotlin版本:
implementation "androidx.navigation:navigation-fragment-ktx:2.4.1" implementation "androidx.navigation:navigation-ui-ktx:2.4.1"
Compose版本:
implementation "androidx.navigation:navigation-compose:2.4.1"
Navigation的主要因素
Navigation Graph,新型的XML資源文件,其中包含App所有的頁面,以及頁面之間的關(guān)系。NavHostFragment,特殊的Fragment,我們可以認為是一個Fragment的容器,Navigation Graph中的Fragment都是通過NavHostFragment進行展示。NavController,在代碼當(dāng)中完成Navigation Graph中的頁面切換工作。
Navigation代碼示例
1.在res創(chuàng)建navigation資源文件夾,在navigation創(chuàng)建nav_graph.xml文件。
2.在activity_main.xml,內(nèi)容詳情如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>
</LinearLayout>屬性說明如下:
android:name="androidx.navigation.fragment.NavHostFragment",告訴系統(tǒng)這是一個特殊的Fragment。app:defaultNavHost="true",表示Fragment會自動處理系統(tǒng)返回鍵,當(dāng)用戶做返回操作的時候會退出當(dāng)前的Fragment.app:navGraph="@navigation/nav_graph",導(dǎo)航視圖。
在nav_graph.xml創(chuàng)建兩個Fragment
<?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"
android:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.yb.test.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.yb.test.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" />
</navigation>NavControIIer實現(xiàn)導(dǎo)航功能
我們在MainFragment中創(chuàng)建按鈕,設(shè)置按鈕的點擊事件:
view.findViewById<Button>(R.id.btn).setOnClickListener {
Navigation.findNavController(it).navigate(R.id.action_mainFragment_to_secondFragment)
}大概的實現(xiàn)就是這樣子,感興趣的小伙伴可以嘗試一下。
以上就是Android-Jetpack-Navigation組件使用示例的詳細內(nèi)容,更多關(guān)于Android-Jetpack-Navigation組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
FFmpeg Principle學(xué)習(xí)open_output_file打開輸出文件
這篇文章主要為大家介紹了FFmpeg Principle學(xué)習(xí)open_output_file打開輸出文件示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Android Messenger實現(xiàn)進程間雙向通信
這篇文章主要為大家詳細介紹了Messenger實現(xiàn)進程間雙向通信,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05

