欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文詳解Jetpack?Android新一代導(dǎo)航管理Navigation

 更新時(shí)間:2023年03月15日 11:38:33   作者:BennuCTech  
這篇文章主要為大家介紹了Jetpack?Android新一代導(dǎo)航管理Navigation詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

不知道小伙伴們是否注意到,用AS創(chuàng)建一個(gè)默認(rèn)的新項(xiàng)目后,MainActivity已經(jīng)有了很大的不同,最大的區(qū)別就是新增加了兩個(gè)Fragment,同時(shí)我們注意到這兩個(gè)Fragment之間跳轉(zhuǎn)的時(shí)候并沒(méi)有使用之前FragmentTransaction這種形式,而是使用了NavController和NavHostFragment,這就是新一代導(dǎo)航管理————Navigation。

項(xiàng)目中依賴Navigation:

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

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

新建一個(gè)Android Resource File,類型選擇Navigation即可,輸入名稱后我們就創(chuàng)建了一個(gè)導(dǎo)航視圖。

在導(dǎo)航試圖中,我們可以通過(guò)添加activity/fragment等標(biāo)簽手動(dòng)添加頁(yè)面,也支持在Design頁(yè)面中通過(guò)界面添加,如下:

注意:這樣添加后手動(dòng)修改一下label。如果我們將Navigation與ToolBar連接,會(huì)在標(biāo)題欄這個(gè)label。

示例中添加了兩個(gè)頁(yè)面,添加后代碼如下:

<?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">
    <fragment
        android:id="@+id/FirstFragment"
        android:name="com.xxx.xxx.FirstFragment"
        android:label="@string/first_fragment_label"
        tools:layout="@layout/fragment_first">
    </fragment>
    <fragment
        android:id="@+id/SecondFragment"
        android:name="com.xxx.xxx.SecondFragment"
        android:label="@string/second_fragment_label"
        tools:layout="@layout/fragment_second">
    </fragment>
</navigation>

除了添加Fragment和Activity,Google還提供了一個(gè)占位符placeholder,添加加完代碼如下:

<fragment android:id="@+id/placeholder" />

用于暫時(shí)占位以便后面可以替換為Fragment和Activity

添加完頁(yè)面后,我們還需要添加頁(yè)面之間的導(dǎo)航,可以手動(dòng)添加action標(biāo)簽,當(dāng)然也可以通過(guò)拖拽來(lái)實(shí)現(xiàn),如下:

這樣我們就添加了一個(gè)從FirstFragment導(dǎo)航到SecondFragment的動(dòng)作,我們?cè)偬砑右粋€(gè)逆向的動(dò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">
    <fragment
        android:id="@+id/FirstFragment"
        android:name="com.xxx.xxx.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.xxx.xxx.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>

注意占位符placeholder同樣支持添加導(dǎo)航。

這樣就實(shí)現(xiàn)了兩個(gè)頁(yè)面間的導(dǎo)航,最后還需要為這個(gè)navigation設(shè)置id和默認(rèn)頁(yè)面startDestination,如下:

<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">

這樣導(dǎo)航視圖就創(chuàng)建完成了??梢钥吹紾oogle力圖通過(guò)可視化工具來(lái)簡(jiǎn)化開(kāi)發(fā)工作,這對(duì)我們開(kāi)發(fā)者來(lái)說(shuō)非常有用,可以省去大量編寫同質(zhì)化代碼的時(shí)間。

添加NavHost

下一步我們需要向Activity中添加導(dǎo)航宿主,導(dǎo)航宿主是一個(gè)空頁(yè)面,必須實(shí)現(xiàn)NavHost接口,我們使用Navigation提供的默認(rèn)NavHost————NavHostFragment即可。如下:

<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_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/nav_graph" />

在Activity的視圖中添加一個(gè)fragment標(biāo)簽,android:name設(shè)置為實(shí)現(xiàn)類,即NavHostFragment;app:navGraph設(shè)置為剛才新建的導(dǎo)航視圖。

注意app:defaultNavHost="true",設(shè)置為true后表示將這個(gè)NavHostFragment設(shè)置為默認(rèn)導(dǎo)航宿主,這樣就會(huì)攔截系統(tǒng)的返回按鈕事件。同一布局中如果有多個(gè)導(dǎo)航宿主(比如雙窗口)則必須制定一個(gè)為默認(rèn)的導(dǎo)航宿主。

這時(shí)候我們運(yùn)行應(yīng)用,就可以發(fā)現(xiàn)Activity中已經(jīng)可以展示FirstFragment了。

導(dǎo)航

我們還需要為兩個(gè)fragment添加按鈕,是其點(diǎn)擊跳轉(zhuǎn)到另外一個(gè)頁(yè)面,代碼如下:

binding.buttonFirst.setOnClickListener {
    findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
}

示例中是FirstFragment中的一個(gè)按鈕,點(diǎn)擊時(shí)執(zhí)行了id為action_FirstFragment_to_SecondFragment的動(dòng)作,這個(gè)是我們之前在導(dǎo)航視圖中配置好的,會(huì)導(dǎo)航到SecondFragment。

注意首先通過(guò)findNavController()來(lái)獲取一個(gè)NavController對(duì)象,然后調(diào)用它的navigate函數(shù)即可,當(dāng)然這個(gè)函數(shù)有多種重載,比如可以傳遞參數(shù),如下:

public void navigate(@IdRes int resId, @Nullable Bundle args) {

這里不一一列舉了,大家自行查看源碼即可。

可以看到使用Navigation代碼精簡(jiǎn)了很多,只需要一行代碼執(zhí)行一個(gè)函數(shù)即可。

findNavController

我們重點(diǎn)來(lái)看看findNavController(),它是一個(gè)擴(kuò)展函數(shù),如下:

fun Fragment.findNavController(): NavController =
        NavHostFragment.findNavController(this)

實(shí)際上是NavHostFragment的一個(gè)靜態(tài)函數(shù)findNavController:

@NonNull
public static NavController findNavController(@NonNull Fragment fragment) {
    ...
    View view = fragment.getView();
    if (view != null) {
        return Navigation.findNavController(view);
    }
    // For DialogFragments, look at the dialog's decor view
    Dialog dialog = fragment instanceof DialogFragment
            ? ((DialogFragment) fragment).getDialog()
            : null;
    if (dialog != null && dialog.getWindow() != null) {
        return Navigation.findNavController(dialog.getWindow().getDecorView());
    }
    throw new IllegalStateException("Fragment " + fragment
            + " does not have a NavController set");
}

通過(guò)源碼可以看到最終是執(zhí)行了Navigation的findNavController函數(shù),它的代碼如下:

@NonNull
public static NavController findNavController(@NonNull View view) {
    NavController navController = findViewNavController(view);
    ...
    return navController;
}

這里是通過(guò)findViewNavController函數(shù)來(lái)獲取NavController的,它的代碼如下:

@Nullable
private static NavController findViewNavController(@NonNull View view) {
    while (view != null) {
        NavController controller = getViewNavController(view);
        if (controller != null) {
            return controller;
        }
        ViewParent parent = view.getParent();
        view = parent instanceof View ? (View) parent : null;
    }
    return null;
}

這里可以看到通過(guò)view來(lái)獲取NavController,如果沒(méi)有則向上層查找(父view)直到找到或到根結(jié)點(diǎn)。getViewNavController代碼如下:

@Nullable
private static NavController getViewNavController(@NonNull View view) {
    Object tag = view.getTag(R.id.nav_controller_view_tag);
    NavController controller = null;
    if (tag instanceof WeakReference) {
        controller = ((WeakReference<NavController>) tag).get();
    } else if (tag instanceof NavController) {
        controller = (NavController) tag;
    }
    return controller;
}

看到這里獲取view中key為R.id.nav_controller_view_tag的tag,這個(gè)tag就是NavController,那么這個(gè)tag又從哪來(lái)的?

其實(shí)就是上面我們提到導(dǎo)航宿主————NavHostFragment,在他的onViewCreated中可以看到如下代碼:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (!(view instanceof ViewGroup)) {
        throw new IllegalStateException("created host view " + view + " is not a ViewGroup");
    }
    Navigation.setViewNavController(view, mNavController);
    // When added programmatically, we need to set the NavController on the parent - i.e.,
    // the View that has the ID matching this NavHostFragment.
    if (view.getParent() != null) {
        mViewParent = (View) view.getParent();
        if (mViewParent.getId() == getId()) {
            Navigation.setViewNavController(mViewParent, mNavController);
        }
    }
}

這里的mNavController是在NavHostFragment的onCreate中創(chuàng)建出來(lái)的,是一個(gè)NavHostController對(duì)象,它繼承NavController,所以就是NavController。

可以看到onViewCreated中調(diào)用了Navigation的setViewNavController函數(shù),它的代碼如下:

public static void setViewNavController(@NonNull View view,
        @Nullable NavController controller) {
    view.setTag(R.id.nav_controller_view_tag, controller);
}

這樣就將NavController加入tag中了,通過(guò)findNavController()就可以得到這個(gè)NavController來(lái)執(zhí)行導(dǎo)航了。

注意在onViewCreated中不僅為Fragment的View添加了tag,同時(shí)還為其父View也添加了,這樣做的目的是在Activity中也可以獲取到NavController,這點(diǎn)下面就會(huì)遇到。

ToolBar

Google提供了Navigation與ToolBar連接的功能,代碼如下:

val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)

上面我們提到,如果Navigation與ToolBar連接,標(biāo)題欄會(huì)自動(dòng)顯示在導(dǎo)航視圖中設(shè)定好的label。

注意這里的findNavController是Activity的擴(kuò)展函數(shù),它最終一樣會(huì)調(diào)用Navigation的對(duì)應(yīng)函數(shù),所以與Fragment的流程是一樣的。而上面我們提到了,在NavHostFragment中給上層View也設(shè)置了tag,所以在這里才能獲取到NavController。

除了這個(gè),我們還可以發(fā)現(xiàn)當(dāng)在切換頁(yè)面的時(shí)候,標(biāo)題欄的返回按鈕也會(huì)自動(dòng)顯示和隱藏。當(dāng)導(dǎo)航到第二個(gè)頁(yè)面SecondFragment,返回按鈕顯示;當(dāng)回退到首頁(yè)時(shí),返回按鈕隱藏。

但是此時(shí)返回按鈕點(diǎn)擊無(wú)效,因?yàn)槲覀冞€需要重寫一個(gè)函數(shù):

override fun onSupportNavigateUp(): Boolean {
    val navController = findNavController(R.id.nav_host_fragment_content_main)
    return navController.navigateUp(appBarConfiguration)
            || super.onSupportNavigateUp()
}

這樣當(dāng)點(diǎn)擊標(biāo)題欄的返回按鈕時(shí),會(huì)執(zhí)行NavController的navigateUp函數(shù),就會(huì)退回到上一頁(yè)面。

總結(jié)

可以看出通過(guò)Google推出的這個(gè)Navigation,可以讓開(kāi)發(fā)者更加優(yōu)雅管理導(dǎo)航,同時(shí)也簡(jiǎn)化了這部分的開(kāi)發(fā)工作,可視化功能可以讓開(kāi)發(fā)者更直觀的進(jìn)行管理。除此之外,Google還提供了Safe Args Gradle插件,該插件可以生成簡(jiǎn)單的對(duì)象和構(gòu)建器類,這些類支持在目的地之間進(jìn)行類型安全的導(dǎo)航和參數(shù)傳遞。關(guān)于這個(gè)大家可以參考官方文檔developer.android.google.cn/guide/navig… 即可。

以上就是一文詳解Jetpack Android新一代導(dǎo)航管理Navigation的詳細(xì)內(nèi)容,更多關(guān)于Jetpack Android導(dǎo)航管理Navigation的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論