Android Navigation 使用总结
程序员文章站
2022-03-08 15:57:17
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
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://blog.csdn.net/qq_33431394/article/details/110631557
上一篇: 微博会员年卡新低价:60元
下一篇: iOS文档下载功能
推荐阅读
-
Android自定义View 使用PathMeasure简单模仿系统ProgressBar(四)
-
Android studio怎么使用git获取最新内容然后合并?
-
android使用DataBinding来设置空状态
-
详解在React项目中安装并使用Less(用法总结)
-
Android studio使用git代码追溯到上个文件的教程?
-
Android 使用Pull方法解析XML文件的方法
-
Android控件系列之Shape使用方法
-
android UI进阶之android中隐藏的layout 抽屉的使用方法
-
Android开发笔记之:一分钟学会使用Logcat调试程序的详解
-
android使用Messenger绑定Service的多种实现方法