1.導包
implementation ‘androidx.navigation:navigation-fragment-ktx:2.3.1’
implementation ‘androidx.navigation:navigation-ui-ktx:2.3.1’
2.1 建立 Navigation 入口
在Activity布局檔案中建立Fragmeng,必須位NavHostFragment
<fragment
android:id="@+id/nav_register_frag"
android:fitsSystemWindows="true"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
app:defaultNavHost="true"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_register" />
- app:defaultNavHost="true"
設定 Navigation的回傳模式,true表明Fragment之間按回傳鍵 回傳上一個Fragment;false 表示直接退出Activity, - app:navGraph="@navigation/nav_register"
指定的 Navigation xml 檔案,
2.2 新建 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/nav_register"
app:startDestination="@id/enterPhoneFragment">
<fragment
android:id="@+id/enterPhoneFragment"
android:name="com.pqtel.pqsecuritysaw.ui.reg.EnterPhoneFragment"
android:label="EnterPhoneFragment"
tools:layout="@layout/frag_enter_phone">
<action
android:id="@+id/action_enterPhoneFragment_to_enterCodeFragment"
app:destination="@id/enterCodeFragment" />
</fragment>
<fragment
android:id="@+id/enterCodeFragment"
android:name="com.pqtel.pqsecuritysaw.ui.reg.EnterCodeFragment"
android:label="EnterCodeFragment"
tools:layout="@layout/frag_enter_code">
<action
app:popUpTo="@id/enterCodeFragment"
app:popUpToInclusive="true"
android:id="@+id/action_enterCodeFragment_to_enterPwdFragment"
app:destination="@id/enterPwdFragment" />
</fragment>
<fragment
android:id="@+id/enterPwdFragment"
android:name="com.pqtel.pqsecuritysaw.ui.reg.EnterPwdFragment"
android:label="EnterPwdFragment"
tools:layout="@layout/frag_enter_pwd" />
</navigation>
- app:startDestination="@id/enterPhoneFragment"
主入口,也就是首頁面必須指定 否則報錯, - app:destination="@id/enterCodeFragment"
Action 跳轉動作 跳轉的目的Fragment - app:popUpTo="@id/enterCodeFragment"
app:popUpToInclusive="true"
一般一起使用如果設定了app:defaultNavHost="true"
理論上的跳轉A-B-C,按下回傳時候是C-B-A
若B到C的Action設定了這兩條屬性,則回傳變成了C-A,B被出堆疊了,

2.3 BottomNavigation與Navigation
可以用此組合替代 viewpager和Tablayout
activity.xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
navigation.xml
<fragment
android:id="@+id/navigation_home"
android:name="com.pqtel.navtest.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.pqtel.navtest.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.pqtel.navtest.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
注意這里的fragment id 需要與menuitem id 一一對用,否則無法跳轉
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/230747.html
標籤:其他
上一篇:騰訊bugly熱更新接入
