天天看點

導航: 嵌套導航圖和 <include> | MAD Skills

嵌套導航圖

我們從導航圖開始。嵌套圖允許您在父導航圖中将一系列目的地頁面分組。

我們看一眼導航圖,coffeeList 和 coffeeEntryDialog 目的地頁面非常适合轉換為嵌套圖。要達成這個目的,我這裡長按 shift 并且同時選擇 "Move to Nested Graph" (移動到嵌套圖):

将 coffeeList 和 coffeeEntryDialogFragment 移動到嵌套圖

現在我們回到代碼界面,您可以看到嵌套圖僅僅是根圖中的新導航圖:

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

   <fragment
       android:id="@+id/donutList"
       android:name="com.android.samples.donuttracker.donut.DonutList"
       android:label="@string/donut_list" >
       <action
           android:id="@+id/action_donutList_to_donutEntryDialogFragment"
           app:destination="@id/donutEntryDialogFragment" />
       <action
           android:id="@+id/action_donutList_to_selectionFragment"
           app:destination="@id/selectionFragment" />
   </fragment>
   <dialog
       android:id="@+id/donutEntryDialogFragment"
       android:name="com.android.samples.donuttracker.donut.DonutEntryDialogFragment"
       android:label="DonutEntryDialogFragment">
       <deepLink app:uri="myapp://navdonutcreator.com/donutcreator" />
       <argument
           android:name="itemId"
           app:argType="long"
           android:defaultValue="-1L" />
   </dialog>
   <fragment
       android:id="@+id/selectionFragment"
       android:name="com.android.samples.donuttracker.setup.SelectionFragment"
       android:label="@string/settings"
       tools:layout="@layout/fragment_selection" >
       <action
           android:id="@+id/action_selectionFragment_to_donutList"
           app:destination="@id/donutList" />
   </fragment>
   <navigation
       android:id="@+id/coffeeGraph"
       app:startDestination="@id/coffeeList">
       <fragment
           android:id="@+id/coffeeList"
           android:name="com.android.samples.donuttracker.coffee.CoffeeList"
           android:label="@string/coffee_list">
           <action
               android:id="@+id/action_coffeeList_to_coffeeEntryDialogFragment"
               app:destination="@id/coffeeEntryDialogFragment" />
       </fragment>
       <dialog
           android:id="@+id/coffeeEntryDialogFragment"
           android:name="com.android.samples.donuttracker.coffee.CoffeeEntryDialogFragment"
           android:label="CoffeeEntryDialogFragment">
           <argument
               android:name="itemId"
               android:defaultValue="-1L"
               app:argType="long" />
       </dialog>
   </navigation>
</navigation>           

所選擇的 Fragment 之間的導航被遷移至嵌套圖中。

嵌套圖必須包含 id。您可以使用這個 id 實作導航到嵌套圖的代碼,但并不是直接轉換到其子目的地頁面。嵌套圖包含自己的啟動目的地頁面,并且請不要分開暴露它們的子目的地頁面。

<navigation
   android:id="@+id/coffeeGraph"
   app:startDestination="@id/coffeeList">           

如果您輕按兩下嵌套圖,就可以發現嵌套的目的地頁面和它們之間的操作。

Include 标簽

除了使用嵌套圖之外,我還可以提取圖到新的導航 xml 檔案中。我在這裡建立了一個新的 xml 檔案,名稱為 coffee_graph,并且将嵌套圖的内容遷移到這個檔案中。

<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/coffeeGraph"
   app:startDestination="@id/coffeeList">
   <fragment
       android:id="@+id/coffeeList"
       android:name="com.android.samples.donuttracker.coffee.CoffeeList"
       android:label="@string/coffee_list">
       <action
           android:id="@+id/action_coffeeList_to_coffeeEntryDialogFragment"
           app:destination="@id/coffeeEntryDialogFragment" />
   </fragment>
   <dialog
       android:id="@+id/coffeeEntryDialogFragment"
       android:name="com.android.samples.donuttracker.coffee.CoffeeEntryDialogFragment"
       android:label="CoffeeEntryDialogFragment">
       <argument
           android:name="itemId"
           android:defaultValue="-1L"
           app:argType="long" />
   </dialog>
</navigation>           

我可以通過 include 标簽将新的圖嵌套到其他檔案中。雖然使用 include 标簽在功能上與使用嵌套圖相同,但您還可以使用其他項目子產品或者庫項目的圖。

<include app:graph="@navigation/coffee_graph"/>           

和嵌套圖相類似,引用的圖不會暴露目的地頁面的清單,也就是說我需要更新菜單 id 來指向 coffeeList。

<item
   android:id="@id/coffeeGraph"
   android:icon="@drawable/coffee_cup"
   android:title="@string/coffee_name" />           

這裡我更新了菜單以使用引用圖的 id。由于 CoffeeList 是所引用圖的起始頁面,是以我可以使用圖 id 來導航到這個圖。如果您現在試着運作應用,所有的功能會和之前一樣。

現在咖啡記錄的導航圖已經實作分離,我們可以對應用進行子產品化處理,順便可以看一下在子產品之間導航的效果如何。

如果您希望同步操作,可以檢查 代碼,裡面包含了到目前為止我所做的全部修改。我建立了兩個新的子產品: core 和 coffee。我将所有常用的類遷移到 core 子產品中,比如 Donut、Coffee、DAO、Database 以及其他常見資源。

接下來,我将所有在咖啡記錄中用到的 fragment、viewModel 和 adapter 類遷移到 coffee 子產品中。在咖啡記錄中用到的布局和其他資源也遷移到這裡,包括 coffee_graph。

如果你想了解更多關于前後端的技術内容,可以通過技術開發平台:

廈門在乎科技

-專注小程式開發、

廈門app開發公司

、網站開發

繼續閱讀