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="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://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 一一对用,否则无法跳转
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/890920.html