Android Navigation 使用总结

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”
    app:popUpTo 填自己即把自己出栈
    理论上的跳转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 一一对用,否则无法跳转

2.4 Navigation 跳转

NavHostFragment
.findNavController(this)
.navigate(id)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android Navigation 是一个用于管理应用程序导航的框架,它可以帮助您轻松地实现常见的导航模式,例如抽屉导航、选项卡导航、底部导航和导航折叠等。 以下是使用 Android Navigation 进行导航的一般步骤: 1. 添加 Navigation 组件库:在 build.gradle 文件中添加以下依赖项: ``` dependencies { def nav_version = "2.3.1" implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version" } ``` 2. 创建导航图:在 res 文件夹中创建一个名为 nav_graph.xml 的 XML 文件。在此文件中,您可以定义应用程序的所有目标目的地以及它们之间的导航关系。 3. 配置 NavHost:在您的布局文件中添加一个 NavHostFragment,它将负责管理您的应用程序的导航。例如: ``` <fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:navGraph="@navigation/nav_graph" /> ``` 4. 配置导航栏:在您的 Activity 或 Fragment 中使用 Navigation UI 库配置您的导航栏。例如: ``` val navController = findNavController(R.id.nav_host_fragment) bottom_nav.setupWithNavController(navController) ``` 5. 导航:使用 NavController 对象执行导航操作。例如,您可以使用以下代码在应用程序中导航到目标目的地: ``` findNavController().navigate(R.id.action_homeFragment_to_detailFragment) ``` 这是一个简单的例子,但是使用 Android Navigation 进行导航可以更加复杂。您可以在官方文档中找到更多信息和示例:https://developer.android.com/guide/navigation/
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值