使用NavigationUI更新UI组件
導(dǎo)航架構(gòu)組件包括一個(gè)NavigationUI 類。此類包含使用頂部應(yīng)用欄,導(dǎo)航抽屜和底部導(dǎo)航管理導(dǎo)航的靜態(tài)方法。
一、聽取導(dǎo)航事件
與the進(jìn)行交互NavController 是在目的地之間導(dǎo)航的主要方法。該NavController 負(fù)責(zé)更換的內(nèi)容NavHost 與新的目的地。在許多情況下,UI元素 - 例如頂級應(yīng)用欄或其他持久性導(dǎo)航控件在BottomNavigationBar外部也是如此 - NavHost需要在目標(biāo)之間導(dǎo)航時(shí)進(jìn)行更新。
NavController提供了一個(gè)OnDestinationChangedListener當(dāng)被稱為接口NavController的當(dāng)前目標(biāo) 或它的參數(shù)改變。可以通過該addOnDestinationChangedListener() 方法注冊新的偵聽器 ,如下面的示例所示。請注意,在調(diào)用時(shí) addOnDestinationChangedListener(),如果當(dāng)前目標(biāo)存在,則會立即將其發(fā)送給您的偵聽器。
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {@Overridepublic void onNavigated(@NonNull NavController navController,@NonNull NavDestination destination,@Nullable Bundle arguments) {textView.setText(destination.getLabel());} }); 復(fù)制代碼NavigationUI用于OnDestinationChangedListener使這些常見的UI組件具有導(dǎo)航感知功能。但請注意,您也可以單獨(dú)使用 OnDestinationChangedListener它來使任何自定義UI或業(yè)務(wù)邏輯了解導(dǎo)航事件。
二、 熱門應(yīng)用欄
頂部應(yīng)用欄在應(yīng)用頂部 提供了一致的位置,用于顯示當(dāng)前屏幕的信息和操作。
NavigationUI包含在用戶瀏覽應(yīng)用時(shí)自動(dòng)更新頂部應(yīng)用欄中內(nèi)容的方法。例如,NavigationUI使用導(dǎo)航圖中的目標(biāo)標(biāo)簽可以使頂部應(yīng)用欄的標(biāo)題保持最新。使用NavigationUI下面討論的頂級應(yīng)用欄方法時(shí),可以使用{argName}標(biāo)簽中的格式從提供給目標(biāo)的參數(shù)中自動(dòng)填充附加到目標(biāo)的標(biāo)簽。
NavigationUI 提供以下頂級應(yīng)用欄類型的支持:
1.Toolbar 2.CollapsingToolbarLayout 3.ActionBar AppBarConfiguration
三 、AppBarConfiguration
NavigationUI使用AppBarConfiguration 對象來管理應(yīng)用程序顯示區(qū)域左上角的“導(dǎo)航”按鈕的行為。默認(rèn)情況下,當(dāng)用戶位于導(dǎo)航圖的頂級目標(biāo)位置時(shí),導(dǎo)航按鈕將隱藏,并在任何其他目標(biāo)位置顯示為“向上”按鈕。
要將導(dǎo)航圖的起始目的地用作唯一的頂級目的地,您可以創(chuàng)建一個(gè)AppBarConfiguration對象并傳入相應(yīng)的導(dǎo)航圖,如下所示:
val appBarConfiguration = AppBarConfiguration(navController.graph)) 復(fù)制代碼如果要自定義哪些目標(biāo)被視為頂級目標(biāo),則可以將一組目標(biāo)ID傳遞給構(gòu)造函數(shù),如下所示:
val appBarConfiguration = AppBarConfiguration(setOf(R.id.main, R.id.android)) 復(fù)制代碼四、創(chuàng)建工具欄
要?jiǎng)?chuàng)建工具欄NavigationUI,首先在主活動(dòng)中定義欄,如下所示:
<LinearLayout><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar" /><fragmentandroid:id="@+id/nav_host_fragment"... />... </LinearLayout> 復(fù)制代碼接下來,setupWithNavController() 從您的主要活動(dòng)的onCreate()方法調(diào)用,如下所示:
override fun onCreate(savedInstanceState: Bundle?) {setContentView(R.layout.activity_main)...val navController = findNavController(R.id.nav_host_fragment)val appBarConfiguration = AppBarConfiguration(navController.graph)findViewById<Toolbar>(R.id.toolbar).setupWithNavController(navController, appBarConfiguration) } 復(fù)制代碼注意:使用a時(shí)Toolbar,導(dǎo)航會自動(dòng)處理“導(dǎo)航”按鈕的單擊事件,因此您無需覆蓋onSupportNavigateUp()。
包括CollapsingToolbarLayout
要CollapsingToolbarLayout在工具欄中添加a ,請先在主要活動(dòng)中定義工具欄和周圍布局,如下所示:
<LinearLayout><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="@dimen/tall_toolbar_height"><android.support.design.widget.CollapsingToolbarLayoutandroid:id="@+id/collapsing_toolbar_layout"android:layout_width="match_parent"android:layout_height="match_parent"app:contentScrim="?attr/colorPrimary"app:expandedTitleGravity="top"app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"app:layout_collapseMode="pin"/></android.support.design.widget.CollapsingToolbarLayout></android.support.design.widget.AppBarLayout><fragmentandroid:id="@+id/nav_host_fragment"... />... </LinearLayout> 復(fù)制代碼接下來,setupWithNavController() 從您的主要活動(dòng)的onCreate方法調(diào)用,如下所示
override fun onCreate(savedInstanceState: Bundle?) {setContentView(R.layout.activity_main)...val layout = findViewById<CollapsingToolbarLayout>(R.id.collapsing_toolbar_layout)val toolbar = findViewById<Toolbar>(R.id.toolbar)val navController = findNavController(R.id.nav_host_fragment)val appBarConfiguration = AppBarConfiguration(navController.graph)layout.setupWithNavController(toolbar, navController, appBarConfiguration) } 復(fù)制代碼五、行動(dòng)吧
要使用默認(rèn)操作欄包含導(dǎo)航支持,請setupActionBarWithNavController() 從主活動(dòng)的onCreate()方法中調(diào)用 ,如下所示。請注意,您需要聲明您的AppBarConfiguration外部onCreate(),因?yàn)槟诟采w時(shí)也使用它onSupportNavigateUp():
private lateinit var appBarConfiguration: AppBarConfiguration ... override fun onCreate(savedInstanceState: Bundle?) {...val navController = findNavController(R.id.nav_host_fragment)appBarConfiguration = AppBarConfiguration(navController.graph)setupActionBarWithNavController(navController, appBarConfiguration) } 復(fù)制代碼接下來,覆蓋onSupportNavigateUp()以處理向上導(dǎo)航:
override fun onSupportNavigateUp(): Boolean {return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() } 復(fù)制代碼六、將目的地綁定到菜單項(xiàng)
NavigationUI還提供了將目標(biāo)綁定到菜單驅(qū)動(dòng)的UI組件的幫助程序。NavigationUI包含一個(gè)輔助方法,onNavDestinationSelected()它MenuItem與NavController托管關(guān)聯(lián)目標(biāo)的方法 一起使用 。如果id在的MenuItem比賽的id目標(biāo)時(shí),NavController可以然后導(dǎo)航到目的地。
作為一個(gè)例子,下面的XML片斷定義一個(gè)菜單項(xiàng),并具有共同的目的地id,details_page_fragment:
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"xmlns:android="http://schemas.android.com/apk/res/android"... >...<fragment android:id="@+id/details_page_fragment"android:label="@string/details"android:name="com.example.android.myapp.DetailsFragment" /> </navigation> <menu xmlns:android="http://schemas.android.com/apk/res/android">...<itemandroid:id="@id/details_page_fragment"android:icon="@drawable/ic_details"android:title="@string/details" /> </menu> 復(fù)制代碼onCreateOptionsMenu()例如,如果您的菜單是通過活動(dòng)添加的,則可以通過覆蓋onOptionsItemSelected()要呼叫的活動(dòng)來將菜單項(xiàng)與目的地相關(guān)聯(lián)onNavDestinationSelected(),如下所示:
override fun onOptionsItemSelected(item: MenuItem): Boolean {val navController = findNavController(R.id.nav_host)return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item) } 復(fù)制代碼現(xiàn)在,當(dāng)用戶單擊details_page_fragment菜單項(xiàng)時(shí),應(yīng)用程序會自動(dòng)導(dǎo)航到相應(yīng)的目標(biāo)位置id。
七、添加導(dǎo)航抽屜
導(dǎo)航抽屜是一個(gè)UI面板,顯示應(yīng)用程序的主導(dǎo)航菜單。當(dāng)用戶觸摸
應(yīng)用欄中的抽屜圖標(biāo)或用戶從屏幕的左邊緣滑動(dòng)手指時(shí),抽屜出現(xiàn)抽屜圖標(biāo)顯示在使用a的所有頂級目標(biāo)上DrawerLayout。頂級目標(biāo)是應(yīng)用程序的根級目標(biāo)。它們不會在應(yīng)用欄中顯示“向上”按鈕。
要添加導(dǎo)航抽屜,請先將a聲明 DrawerLayout為根視圖。在其中DrawerLayout,添加主UI內(nèi)容的布局和包含導(dǎo)航抽屜內(nèi)容的另一個(gè)視圖。
例如,以下布局使用DrawerLayout帶有兩個(gè)子視圖:a NavHostFragment用于包含主要內(nèi)容,a NavigationView 用于包含 導(dǎo)航抽屜的內(nèi)容
<?xml version="1.0" encoding="utf-8"?> <!-- Use DrawerLayout as root container for activity --> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"><!-- Layout to contain contents of main body of screen (drawer will slide over this) --><fragmentandroid:name="androidx.navigation.fragment.NavHostFragment"android:id="@+id/nav_host_fragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:navGraph="@navigation/nav_graph" /><!-- Container for contents of drawer - use NavigationView to make configuration easier --><android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"android:fitsSystemWindows="true" /> </android.support.v4.widget.DrawerLayout> 復(fù)制代碼接下來,DrawerLayout 通過將其傳遞到導(dǎo)航圖,將其連接到AppBarConfiguration,如下所示:
AppBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()) .setDrawerLayout(drawerLayout) .build(); 復(fù)制代碼注意:使用時(shí)NavigationUI,頂部應(yīng)用欄幫助器會在當(dāng)前目標(biāo)更改時(shí)自動(dòng)在抽屜圖標(biāo)和“向上”圖標(biāo)之間切換。你不需要使用 ActionBarDrawerToggle。
想學(xué)習(xí)更多Android知識,或者獲取相關(guān)資料請加入Android開發(fā)交流群:1018342383。 有面試資源系統(tǒng)整理分享,Java語言進(jìn)階和Kotlin語言與Android相關(guān)技術(shù)內(nèi)核,APP開發(fā)框架知識, 360°Android App全方位性能優(yōu)化。Android前沿技術(shù),高級UI、Gradle、RxJava、小程序、Hybrid、 移動(dòng)架構(gòu)師專題項(xiàng)目實(shí)戰(zhàn)環(huán)節(jié)、React Native、等技術(shù)教程!架構(gòu)師課程、NDK模塊開發(fā)、 Flutter等全方面的 Android高級實(shí)踐技術(shù)講解。還有在線答疑
總結(jié)
以上是生活随笔為你收集整理的使用NavigationUI更新UI组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荣耀 Magic5 / Pro 系列发布
- 下一篇: 超 5800 亿美元,微软谷歌神仙打架,