最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Jetpack navigation組件超詳細(xì)講解

 更新時(shí)間:2022年10月31日 14:31:41   作者:幸大叔  
首先Navigation是一個(gè)架構(gòu)組件,因?yàn)榍袚QActivity是一個(gè)Binder通信的過程,所以Activity是屬于比較重的組件。而Fragment的切換其實(shí)只是View的切換,比較輕量級(jí)。因此單Activity加Fragment切換成為了比較常見的架構(gòu)方式

導(dǎo)航是指支持用戶導(dǎo)航、進(jìn)入和退出應(yīng)用中不同內(nèi)容片段的交互。Android Jetpack 的導(dǎo)航組件可幫助您實(shí)現(xiàn)導(dǎo)航,無論是簡單的按鈕點(diǎn)擊,還是應(yīng)用欄和抽屜式導(dǎo)航欄等更為復(fù)雜的模式,該組件均可應(yīng)對。導(dǎo)航組件還通過遵循一套既定原則來確保一致且可預(yù)測的用戶體驗(yàn)。

依賴項(xiàng)

    def nav_version = "2.5.2"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

創(chuàng)建導(dǎo)航圖

導(dǎo)航發(fā)生在應(yīng)用中的各個(gè)目的地之間。這些目的地是通過操作連接的,導(dǎo)航圖是一種資源文件,其中包含您的所有目的地和操作,該圖表會(huì)顯示應(yīng)用的所有導(dǎo)航路徑。

點(diǎn)擊加號(hào)便可以創(chuàng)建目的地Fragment,連接操作由箭頭表示,該箭頭表示用戶可以如何從一個(gè)目的地導(dǎo)航到另一個(gè)目的地。

目的地是指應(yīng)用中的不同內(nèi)容區(qū)域,操作是指目的地之間的邏輯連接,表示用戶可以采取的路徑。然后,我們來看下此時(shí)的導(dǎo)航xml代碼::main_navigation.xml

<?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/main_navigation"
    app:startDestination="@id/oneFragment">
    <fragment
        android:id="@+id/oneFragment"
        android:name="com.example.myapplication.OneFragment"
        android:label="fragment_one"
        tools:layout="@layout/fragment_one" >
        <action
            android:id="@+id/action_oneFragment_to_twoFragment"
            app:destination="@id/twoFragment" />
    </fragment>
    <fragment
        android:id="@+id/twoFragment"
        android:name="com.example.myapplication.TwoFragment"
        android:label="fragment_two"
        tools:layout="@layout/fragment_two" >
        <action
            android:id="@+id/action_twoFragment_to_threeFragment"
            app:destination="@id/threeFragment" />
    </fragment>
    <fragment
        android:id="@+id/threeFragment"
        android:name="com.example.myapplication.ThreeFragment"
        android:label="fragment_three"
        tools:layout="@layout/fragment_three" />
</navigation>

其中,navigation 元素是導(dǎo)航圖的根元素,當(dāng)您向圖表添加目的地和連接操作時(shí),就 action 和 app:destination

導(dǎo)航宿主

導(dǎo)航宿主是一個(gè)空容器,用戶在您的應(yīng)用中導(dǎo)航時(shí),目的地會(huì)在該容器中交換進(jìn)出,導(dǎo)航宿主必須派生于 NavHost,Navigation 組件的默認(rèn) NavHost 實(shí)現(xiàn) (NavHostFragment) 負(fù)責(zé)處理 Fragment 目的地的交換。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_navigation" />
</LinearLayout>

android:name:NavHost 實(shí)現(xiàn)類的名稱

app:navGraph:將 NavHostFragment 與導(dǎo)航圖相關(guān)聯(lián)

app:defaultNavHost=“true”:確保 NavHostFragment 會(huì)攔截系統(tǒng)返回按鈕。

注意:只能有一個(gè)默認(rèn) NavHost,如果同一布局(例如,雙窗格布局)中有多個(gè)托管容器,請務(wù)必僅指定一個(gè)默認(rèn) NavHost

導(dǎo)航到目的地

跳轉(zhuǎn)建議使用 Safe Args 確保類型安全,在 Project 下的 build.gradle 添加。啟用 Safe Args 后,該插件會(huì)生成代碼,其中包含每個(gè)操作的類和方法。對于每個(gè)操作,Safe Args 還會(huì)為每個(gè)源頭生成一個(gè)類。生成的類的名稱由源類的名稱和“Directions”一詞組成,例如OneFragment,生成的就是OneFragmentDirections

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.5.2"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

在APP模塊的 build.gradle 添加

plugins {
    id 'androidx.navigation.safeargs'
}

點(diǎn)擊按鈕,進(jìn)行跳轉(zhuǎn)

        jump.setOnClickListener {
            val action = OneFragmentDirections.actionOneFragmentToTwoFragment()
            it.findNavController().navigate(action)
        }

傳遞參數(shù)

啟用 Safe Args 后,會(huì)創(chuàng)建一個(gè)類,該類的名稱是在目的地的名稱后面加上“Args”,例如 OneFragment,名稱為OneFragmentArgs,用于參數(shù)傳遞。

在 main_navigation.xml 加上參數(shù)標(biāo)簽 argument

    <fragment
        android:id="@+id/oneFragment"
        android:name="com.example.myapplication.OneFragment"
        android:label="fragment_one"
        tools:layout="@layout/fragment_one">
        <argument
            android:name="name"
            android:defaultValue="1"
            app:argType="string" />
        <action
            android:id="@+id/action_oneFragment_to_twoFragment"
            app:destination="@id/twoFragment" />
    </fragment>

在 OneFragment 中進(jìn)行跳轉(zhuǎn),參數(shù)傳遞

        jump.setOnClickListener {
            val param = OneFragmentArgs.Builder().setName(nameStr).build().toBundle()
            it.findNavController().navigate(R.id.action_oneFragment_to_twoFragment, param)
        }

TwoFragment接收參數(shù),建議使用 ktx,就可以使用 by navArgs() 屬性委托來訪問參數(shù)

class TwoFragment : Fragment() {
    private val args: OneFragmentArgs by navArgs()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val nameStr = args.name //接收的值
        return inflater.inflate(R.layout.fragment_two, container, false)
    }
}

NavigationUI

此類包含多種靜態(tài)方法,可幫助您使用頂部應(yīng)用欄、抽屜式導(dǎo)航欄和底部導(dǎo)航欄來管理導(dǎo)航。

這里簡單實(shí)現(xiàn)一個(gè)底部導(dǎo)航欄,先創(chuàng)建一個(gè)menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/oneFragment"
        android:icon="@android:drawable/ic_menu_camera"
        android:title="相機(jī)" />
    <item
        android:id="@+id/twoFragment"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="郵件" />
    <item
        android:id="@+id/threeFragment"
        android:icon="@android:drawable/ic_menu_call"
        android:title="電話" />
</menu>

創(chuàng)建三個(gè)Fragment,OneFragment,TwoFragment 和 ThreeFragment,作為三個(gè)頁面

然后 navigation 長這樣,需要注意的是menu各個(gè)item的id和fragment的id需要相對應(yīng)

<?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/main_navigation"
    app:startDestination="@id/oneFragment">
    <fragment
        android:id="@+id/oneFragment"
        android:name="com.example.myapplication.OneFragment"
        android:label="fragment_one"
        tools:layout="@layout/fragment_one" />
    <fragment
        android:id="@+id/twoFragment"
        android:name="com.example.myapplication.TwoFragment"
        android:label="fragment_two"
        tools:layout="@layout/fragment_two" />
    <fragment
        android:id="@+id/threeFragment"
        android:name="com.example.myapplication.ThreeFragment"
        android:label="fragment_three"
        tools:layout="@layout/fragment_three" />
</navigation>

宿主的布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_navigation" />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/main_item" />
</LinearLayout>

然后在宿主Activity中執(zhí)行如下就行啦

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom)
        NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)

多模塊導(dǎo)航

對于比較大型的項(xiàng)目,我們一般都會(huì)采用組件化開發(fā),每個(gè)功能模塊都專注于一項(xiàng)功能。每個(gè)功能模塊都是一個(gè)獨(dú)立的單元,擁有自己的導(dǎo)航圖和目的地,app 模塊依賴于每個(gè)功能模塊。

app 模塊負(fù)責(zé)提供應(yīng)用的完整導(dǎo)航圖,以及將 NavHost 添加到界面中,可以使用 include 來引用庫圖。

<?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/main_navigation"
    app:startDestination="@id/mainFragment">
    <include app:graph="@navigation/bike_nav" />
    <include app:graph="@navigation/car_nav" />
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.myapplication.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main">
        <action
            android:id="@+id/action_mainFragment_to_car_nav"
            app:destination="@id/car_nav" />
        <action
            android:id="@+id/action_mainFragment_to_bike_nav"
            app:destination="@id/bike_nav" />
    </fragment>
</navigation>

如下圖所示,這里主頁面一個(gè)MainFragment,用來做跳轉(zhuǎn),分別可跳轉(zhuǎn)到兩個(gè)不同的模塊

然后在 MainFragment 里執(zhí)行跳轉(zhuǎn)邏輯

            findViewById<TextView>(R.id.red).setOnClickListener {
                it.findNavController().navigate(MainFragmentDirections.actionMainFragmentToCarNav())
            }
            findViewById<TextView>(R.id.purple).setOnClickListener {
                it.findNavController()
                    .navigate(MainFragmentDirections.actionMainFragmentToBikeNav())
            }

那么,問題來了,如果是兩個(gè)單獨(dú)的功能模塊之間需要導(dǎo)航呢,而獨(dú)立的功能模塊彼此又看不到對方,怎么搞呢?

這時(shí),可以使用深層鏈接直接導(dǎo)航到與隱式深層鏈接關(guān)聯(lián)的目的地

舉個(gè)例子,現(xiàn)在要從 Bikelibrary 里的 Fragment 跳轉(zhuǎn)到 Carlibrary 里的 Fragment

將 Carlibrary 中的導(dǎo)航添加深層鏈接

<?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/car_nav"
    app:startDestination="@id/carFragment">
    <fragment
        android:id="@+id/carFragment"
        android:name="com.example.carlibrary.CarFragment"
        android:label="fragment_car"
        tools:layout="@layout/fragment_car">
        <deepLink
            android:id="@+id/deepLink"
            app:uri="android-app://com.example.carlibrary/carFragment" />
    </fragment>
</navigation>

當(dāng)然,你也可以從xml編輯器的右側(cè)添加

然后在 bikelibrary 里的 fragment 中根據(jù)此鏈接進(jìn)行導(dǎo)航,這里設(shè)置了一個(gè)按鈕的點(diǎn)擊事件

            findViewById<Button>(R.id.bike_to_car).setOnClickListener {
                val request =
                    NavDeepLinkRequest.Builder.fromUri("android-app://com.example.carlibrary/carFragment".toUri())
                        .build()
                it.findNavController().navigate(request)
            }

這樣,就完成了跨模塊導(dǎo)航,是不是很方便呢?不過,需要注意的是,Safe Args 不支持跨模塊導(dǎo)航,因?yàn)闆]有針對目的地的直接操作。

到此這篇關(guān)于Jetpack navigation組件超詳細(xì)講解的文章就介紹到這了,更多相關(guān)Jetpack navigation內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • android開發(fā)教程之使用listview顯示qq聯(lián)系人列表

    android開發(fā)教程之使用listview顯示qq聯(lián)系人列表

    這篇文章主要介紹了android使用listview顯示qq聯(lián)系人列表的示例,需要的朋友可以參考下
    2014-02-02
  • Android編程之ListPreference用法實(shí)例分析

    Android編程之ListPreference用法實(shí)例分析

    這篇文章主要介紹了Android編程之ListPreference用法,結(jié)合實(shí)例形式較為詳細(xì)的分析說明了ListPreference的功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2015-12-12
  • Android性能優(yōu)化之捕獲java crash示例解析

    Android性能優(yōu)化之捕獲java crash示例解析

    這篇文章主要介紹了Android性能優(yōu)化之捕獲java crash示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android拖拽助手ViewDragHelper的創(chuàng)建與使用實(shí)例

    Android拖拽助手ViewDragHelper的創(chuàng)建與使用實(shí)例

    ViewDragHelper是針對 ViewGroup 中的拖拽和重新定位 views 操作時(shí)提供了一系列非常有用的方法和狀態(tài)追蹤,下面這篇文章主要給大家介紹了關(guān)于Android拖拽助手ViewDragHelper的創(chuàng)建與使用的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • pp列表之分組ListView詳解

    pp列表之分組ListView詳解

    本篇文章是對Android中的ListView進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android利用Intent讀取和更新通訊錄

    Android利用Intent讀取和更新通訊錄

    這篇文章主要介紹了Android利用Intent讀取和更新通訊錄的相關(guān)資料,通過用戶配置文件(user profile)讀取和更新該手機(jī)的所有聯(lián)系人信息,需要的朋友可以參考下
    2016-06-06
  • Android整理好的圖片壓縮工具類

    Android整理好的圖片壓縮工具類

    今天小編就為大家分享一篇關(guān)于Android整理好的圖片壓縮工具類,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Flutter 網(wǎng)絡(luò)請求框架封裝詳解

    Flutter 網(wǎng)絡(luò)請求框架封裝詳解

    這篇文章主要介紹了Flutter 網(wǎng)絡(luò)請求框架封裝詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03
  • Android 簡單服務(wù)定位器模式實(shí)現(xiàn)

    Android 簡單服務(wù)定位器模式實(shí)現(xiàn)

    這篇文章主要介紹了Android 簡單服務(wù)定位器模式實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android的SurfaceView和TextureView介紹及使用示例

    Android的SurfaceView和TextureView介紹及使用示例

    SurfaceView 是一種用于直接將圖形繪制到屏幕的Android組件,下面給大家分享SurfaceView使用示例,它展示了如何在 Android 應(yīng)用中創(chuàng)建并使用,感興趣的朋友一起看看吧
    2024-12-12

最新評論

左云县| 邹平县| 法库县| 嘉鱼县| 甘泉县| 太保市| 乌鲁木齐县| 和田市| 白朗县| 晋宁县| 天峻县| 江达县| 奉节县| 梓潼县| 咸丰县| 溧阳市| 万全县| 禄丰县| 五莲县| 泰兴市| 三江| 和静县| 蒙自县| 长岛县| 芜湖市| 固安县| 松潘县| 阜宁县| 万山特区| 拉孜县| 江油市| 新民市| 梧州市| 怀仁县| 上林县| 托克逊县| 辰溪县| 武安市| 政和县| 伽师县| 和林格尔县|