【Jetpack】Navigation 导航组件 ③ ( 为 Navigation Graph 页面跳转 action 添加跳转动画 )

2023-06-27 10:04:24


代码地址 :





一、为 Navigation Graph 添加跳转动画




1、进入 Navigation Graph 配置的 Design 模式


打开 " res/navigation " 下的 Navigation Graph 的 Xml 配置文件 ,

进入 Design 编辑模式 ,

在这里插入图片描述


2、选中 action 跳转


选中一个 代表 Fragment 页面跳转 Action 对应的箭头 , 选中后 , 该箭头变为蓝色 , 右侧可以查看该 跳转 Action 的属性 ,

在基础属性中 , 可以看到 该跳转 action 的 id 为 " action_fragmentA_to_fragmentB " ,

跳转的目的页面为 fragmentB 页面 ;

点击 Animations 属性左侧的三角箭头 , 可以展开 Animations 动画相关的属性 ,

其中 enterAnim 是进入动画 , exitAnim 是退出动画 , 这两个动画选项后面都有一个 " Pick a Resource " 按钮 ;

在这里插入图片描述


3、为 action 跳转设置 enterAnim 进入动画


点击 enterAnim 进入动画 后的 " Pick a Resource " 按钮 , 可以在下面的 " Pick a Resource " 对话框中选择对应的动画 ;

在这里插入图片描述

enterAnim 进入动画 , 可以选择 nav_default_enter_anim 动画 ;

在这里插入图片描述

设置完毕后 , action_fragmentA_to_fragmentB 跳转 action 增加了一个属性 app:enterAnim="@anim/nav_default_enter_anim" ;

    <fragment
        android:id="@+id/fragmentA"
        android:name="kim.hsl.nav.FragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"
            app:enterAnim="@anim/nav_default_enter_anim" />
    </fragment>

4、为 action 跳转设置 exitAnim 退出动画


点击 exitAnim 退出动画 后的 " Pick a Resource " 按钮 , 可以在下面的 " Pick a Resource " 对话框中选择对应的动画 ,

设置 系统自带的 默认退出动画 nav_default_exit_anim 为退出动画 ;

在这里插入图片描述

最终的 FragmentA 的页面配置如下 , 关键关注 action 跳转动作中的 app:enterAnim 进入动画属性 和 app:exitAnim 退出动画属性 ;

    <fragment
        android:id="@+id/fragmentA"
        android:name="kim.hsl.nav.FragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" />
    </fragment>

上述设置了 两个属性后 , 在跳转时 , 才能有 进入 / 退出 页面跳转动画 ;

app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"

5、通过代码为 action 跳转设置进入 / 退出动画


在设置了 FragmentA 的 action_fragmentA_to_fragmentB 跳转动作 action 的 进入 和 退出 动画后 , 代码变为如下样式 :

<?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/navigation_graph"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="kim.hsl.nav.FragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" />
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="kim.hsl.nav.FragmentB"
        android:label="fragment_b"
        tools:layout="@layout/fragment_b" >
        <action
            android:id="@+id/action_fragmentB_to_fragmentA"
            app:destination="@id/fragmentA" />
    </fragment>
</navigation>

进入动画 , 就是在 action 中添加

app:enterAnim="@anim/nav_default_enter_anim"

属性 , 退出动画 , 就是在 action 中添加

app:exitAnim="@anim/nav_default_exit_anim"

属性 ;

现在要为 FragmentB 的 action_fragmentB_to_fragmentA 跳转动作 action 添加跳转动画 , 直接添加这两种属性即可 ;

app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"

最终修改完成后的代码为 :

<?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/navigation_graph"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="kim.hsl.nav.FragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" />
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="kim.hsl.nav.FragmentB"
        android:label="fragment_b"
        tools:layout="@layout/fragment_b" >
        <action
            android:id="@+id/action_fragmentB_to_fragmentA"
            app:destination="@id/fragmentA"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" />
    </fragment>
</navigation>

进入 Design 模式 , 选中 FragmentB 跳转到 FragmentA 的 箭头 , 也就是跳转动作 action , 可以看到 Animations 属性已经设置了 进入 / 退出 跳转动画 ;

在这里插入图片描述


6、执行效果


请添加图片描述


代码地址 :

更多推荐

Mysql表的约束

目录一、NULL约束二、default约束三、comment四、zerofill五、primarykey六、auto_increment七、唯一键八、外键为了保证数据的完整性和可预期性,表中一定要有各种约束,通过约束,让我们未来插入数据库表中的数据是符合预期的。表的约束很多,这里主要介绍以下几个约束:null/notn

软件测试7大误区

随着软件测试对提高软件质量重要性的不断提高,软件测试也不断受到重视。但是,国内软件测试过程的不规范,重视开发和轻视测试的现象依旧存在。因此,对于软件测试的重要性、测试方法和测试过程等方面都存在很多不恰当的认识,这将会进一步的影响软件测试活动的开展,并且阻碍软件测试质量的提高。下面简单列举了几种有代表性的对软件测试的认识

typeof的作用

typeof是JavaScript中的一种运算符,用于获取给定值的数据类型。它的作用是返回一个字符串,表示目标值的数据类型。通过使用typeof运算符,我们可以在运行时确定一个值的类型,从而进行相应的处理或逻辑判断。常见的数据类型包括:"undefined":未定义的值"boolean":布尔值"number":数字"

Java的checked exception有意义吗?

1前言这种异常必须在编译前就try/catch,又不一定会抛异常,小项目中不明显,大项目中,会造成不必要代码臃肿和可读性降低,完全可在编译出错时,通过单元测试和调试,得到正确代码。这设计还有啥意义?CheckedException初衷很好,但事实上是没啥卵用设计。2初衷很好因为我们都知软件会有各种问题,严谨处理这些问题

【JavaScript精通之道】掌握数据遍历:解锁现代化遍历方法,提升开发效率!

​🎬岸边的风:个人主页🔥个人专栏:《VUE》《javaScript》⛺️生活的理想,就是为了理想的生活!​目录📚前言📘1.reduce方法📘2.forEach方法📘3.map方法📘4.for循环📘5.filter方法📘6.for...of循环📘7.Object.keys方法📘8.Object.va

ARTS 打卡 第一周,初试ARTS

前言认识三掌柜的想必都知道,我持续创作技术博客已经有6年时间了,固定每个月发布不少于6篇博文。同时,自己作为一名热爱分享的开发者,像ARTS这样的活动自然少不了我。由于我是打算挤在一起分享,之前都是做了本地文档记录,所以直接把内容整合起来即可,那么接下来就开启我的第一周打卡咯。ARTS是什么?ARTS其实是由四个部分组

ngx_memalign是在 Nginx 中使用的一个内存分配函数,它的作用是根据指定的对齐方式和大小,分配一块对齐的内存

ngx_memalignngx_memalign是在Nginx中使用的一个内存分配函数。它的作用是根据指定的对齐方式和大小,分配一块对齐的内存。这个函数在Nginx的内存管理系统中使用得比较广泛,尤其是在处理大块数据时,可以提高内存访问的效率。具体的函数原型如下:void*ngx_memalign(size_talig

Apache Spark 的基本概念

ApacheSpark是一种快速、可扩展、通用的数据处理引擎。它是一种基于内存的计算框架,支持分布式数据处理、机器学习、图形计算等多种计算任务。与传统的HadoopMapReduce相比,Spark具有更高的性能和更广泛的应用场景。Spark中的基本概念包括:1.ResilientDistributedDatasets

Javas | DecimalFormat类、BigDecimal类、Random类

目录:1.DecimalFormat类2.BigDecimal类3.Random类4.需求:编写程序,生成5个不重复的随机数1.DecimalFormat类DecimalFormat是NumberFormat的一个具体子类,用于格式十进制数字。/***关于数字的格式化*/publicclassDecimalFormat

滚雪球学Java(28):轻松掌握数组:访问和遍历技巧

🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!!前言在Java编程中,数组是一种常用的数据结构。它在存储和处理数据时具有很高的效率,能够方便地进行访问和遍历。本文将介绍数组的访问和遍历技巧,帮助读者更加深入地了解Java数

单片机C语言实例:23、串口通讯

一、轮询发送程序实例1:#include<reg52.h>//包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include"delay.h"/*------------------------------------------------函数声明---------------------------

热文推荐