Android添加item动画,RecyclerView基础篇-Item添加动画

4c1eb409bc0e

Android_Banner.jpg

简介

本节中我们介绍下给RecyclerView中的Item添加动画。

添加的动画,分为,在打开列表时有Item的展示动画,当滑动的时候没有动画

和打开列表滑动时有动画两种

实现过程

实现一个列表

效果如下

4c1eb409bc0e

Screenshot_2020-09-01-17-03-35-349_com.dashingqi.module.recyclerview.png

接下来我们就要操作这个列表中的Item,让其产生动画

布局的实现代码

main_activity.xml 的布局文件

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools">

name="viewModel"

type="com.dashingqi.module.recyclerview.RvAnimationViewModel" />

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".RvAnimationActivity">

android:id="@+id/animRv"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

MainActivity.kt中的代码

lass RvAnimationActivity : AppCompatActivity() {

private val animationDownToUp by lazy {

AnimationUtils.loadAnimation(this, R.anim.item_anim_down_to_up)

}

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

val dataBinding = DataBindingUtil.setContentView(

this,

R.layout.activity_rv_animation

)

//获取到ViewModel的实例

val viewModel = ViewModelProvider(this)[RvAnimationViewModel::class.java]

//绑定ViewModel

dataBinding.viewModel = viewModel

//设置适配器

var adapter = RvAnimationAdapter(viewModel.items, animRv)

animRv.adapter = adapter

// 为Rv中的Item添加装饰器

animRv.addItemDecoration(object : RecyclerView.ItemDecoration() {

override fun getItemOffsets(

outRect: Rect,

view: View,

parent: RecyclerView,

state: RecyclerView.State

) {

val childPosition = parent.getChildAdapterPosition(view)

if (childPosition != 0) {

outRect.top = DensityUtils.dip2pxInt(parent.context, 16f)

}

}

})

}

}

ViewModel中的代码

class RvAnimationViewModel : ViewModel() {

val items = ObservableArrayList()

val itemBinding = ItemBinding.of(BR.item, R.layout.item_anim_view)

init {

for (index in 0 until 80) {

items.add("Item${index}")

}

}

}

Adapter中的代码

class RvAnimationAdapter(var datas: ArrayList, var recyclerView: RecyclerView) :

RecyclerView.Adapter() {

class MyViewHolder(var dataBinding: ItemAnimViewBinding) :

RecyclerView.ViewHolder(dataBinding.root) {

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

val dataBinding = DataBindingUtil.inflate(

LayoutInflater.from(parent.context),

R.layout.item_anim_view,

parent,

false

)

return MyViewHolder(dataBinding)

}

override fun getItemCount(): Int {

return datas.size

}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

val itemDataBinding = holder.dataBinding as ItemAnimViewBinding

itemDataBinding.item = datas[position]

itemDataBinding.executePendingBindings()

}

}

item的布局文件

xmlns:app="http://schemas.android.com/apk/res-auto">

name="item"

type="String" />

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="240dp"

android:background="@android:color/holo_red_dark"

android:text="@{item}"

android:gravity="center"

android:textColor="#ffffff"

android:textSize="20sp"

android:textStyle="bold"

app:layout_constraintTop_toTopOf="parent" />

实现进入列表时的动画

实现效果如下

4c1eb409bc0e

list_start.gif

该动画是从右侧平移到屏幕中,所以我们的平移动画的X轴的起点从 100%开始,终止点为0 ,y不变

android:duration="500">

android:fromXDelta="100%p"

android:fromYDelta="0"

android:toXDelta="0"

android:toYDelta="0" />

android:fromAlpha="0"

android:toAlpha="100" />

接着借助LayoutAnimationController 给 RV的layoutAnimation设置动画数据

val viewModel = ViewModelProvider(this)[RvAnimationViewModel::class.java]

dataBinding.viewModel = viewModel

var adapter = RvAnimationAdapter(viewModel.items, animRv)

animRv.adapter = adapter

var animation = AnimationUtils.loadAnimation(this, R.anim.item_anim_translate)

val layoutAnimationController = LayoutAnimationController(animation)

//设置顺序

layoutAnimationController.order = LayoutAnimationController.ORDER_NORMAL

animRv.layoutAnimation = layoutAnimationController

滑动的时候带有动画

效果如下

4c1eb409bc0e

list_scroll.gif

上图中的效果,是在ItemView可见的时候执行动画,我们的切入点也就是这个时机,正好Adapter中有提供一个方法onViewAttachedToWindow()

onViewAttachedToWindow() 是当Adapter创建好的View依附在Window的时候调用的,所以这个方法是一个时机,在这个方法中,为每一个ItemView设置动画。

同时我们还需要为Rv设置滑动的监听事件,来记录滑动的方向,这样在onViewAttachedToWindow()方法中设置不同的动画

所以Adapter中的代码变更如下

class RvAnimationAdapter(var datas: ArrayList, var recyclerView: RecyclerView) :

RecyclerView.Adapter() {

/**

* 用来记录当前是向上滑动的

*/

var isScrollUp = false

/**

* 用来记录当前是向下滑动的

*/

var isScrollDown = false

/**

* 动画

*/

private val animation by lazy {

AnimationUtils.loadAnimation(recyclerView.context, R.anim.item_anim_translate)

}

private val animationUpToDown by lazy {

AnimationUtils.loadAnimation(recyclerView.context, R.anim.item_anim_up_to_down)

}

private val animationDownToUp by lazy {

AnimationUtils.loadAnimation(recyclerView.context, R.anim.item_anim_down_to_up)

}

init {

//为RV添加滑动事件的监听

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

super.onScrollStateChanged(recyclerView, newState)

}

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

super.onScrolled(recyclerView, dx, dy)

isScrollUp = dy > 0

isScrollDown = dy < 0

}

})

}

class MyViewHolder(var dataBinding: ItemAnimViewBinding) :

RecyclerView.ViewHolder(dataBinding.root) {

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

val dataBinding = DataBindingUtil.inflate(

LayoutInflater.from(parent.context),

R.layout.item_anim_view,

parent,

false

)

return MyViewHolder(dataBinding)

}

override fun getItemCount(): Int {

return datas.size

}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

val itemDataBinding = holder.dataBinding as ItemAnimViewBinding

itemDataBinding.item = datas[position]

itemDataBinding.executePendingBindings()

}

/**

* 当创建好的View依附到Window上时回调的

*/

override fun onViewAttachedToWindow(holder: MyViewHolder) {

super.onViewAttachedToWindow(holder)

for (index in 0 until recyclerView.childCount) {

//获取到Item

val itemView = recyclerView.getChildAt(index)

//清除每一个Item上的动画

itemView?.clearAnimation()

}

// 当向上滑动的时候

if (isScrollUp) {

holder.itemView.startAnimation(animationDownToUp)

}

//当向下滑动的时候

if (isScrollDown) {

holder.itemView.startAnimation(animationUpToDown)

}

}

}

对应的动画文件

android:duration="200">

android:fromYDelta="100%"

android:toYDelta="0" />

android:fromAlpha="0"

android:toAlpha="1" />

android:duration="200"

>

android:fromYDelta="-100%"

android:toYDelta="0" />

android:fromAlpha="0"

android:toAlpha="1" />

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278911.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

geek_Ask How-To Geek:营救受感染的PC,安装无膨胀iTunes和驯服疯狂的触控板

geekYou’ve got questions and we’ve got answers. Today we highlight how to save your computer if it’s so overrun by viruses and malware you can’t work from within Windows, install iTunes without all the bloat, and tame a hyper-sensitive trackpad. 您有问…

第1课:接口测试和jmeter总结

接口测试 1. 接口的分类&#xff1a;webService和http api接口1&#xff09; webService接口&#xff1a;是按照soap协议通过http传输&#xff0c;请求报文和返回报文都是xml格式&#xff0c;一般要借助工具来测试接口&#xff1b;2&#xff09; http api接口&#xff1a;是按照…

极客时间和极客学院_极客历史记录的本周:Twitter的诞生,OS X十周年以及太空停留时间最长的时代即将结束...

极客时间和极客学院Every week we bring you interesting trivia and milestones from the archives of Geekdom. Today we’re taking a peek at the birth of Twitter, ten years of Mac OS X, and the longest space stay in history. 每周&#xff0c;我们都会为您带来有趣…

Android风格ppt,Material Design风格的快手PPT

突发奇想&#xff0c;感觉MD风格既然适合 Android 软件的界面&#xff0c;那么在一般PPT 演示中&#xff0c;效果当也是不错。于是在网上去寻了几处制作贴&#xff0c;也简单看了 MD 设计指南的几处要点。先试试一番再说。关于 MD 设计指南和几处制作贴&#xff0c;我会把链接贴…

dropbox链接过期_询问操作方法:“开始”菜单中的Dropbox,了解符号链接和翻录TV系列DVD...

dropbox链接过期This week we take a look at how to incorporate Dropbox into your Windows Start Menu, understanding and using symbolic links, and how to rip your TV series DVDs right to unique and high-quality episode files. 本周&#xff0c;我们来看看如何将D…

android listpreference 自定义,Android – 我的ListPreference中的自定义行布局

在我的Android应用程序中,我实现了从ListPreference扩展的类SubtitleColorListPreference.我需要这个,因为我需要为列表中的每个项目设置自己的布局.一切正常,它看起来像这样&#xff1a;重要的代码是onPrepareDialogBu​​ilder(AlertDialog.Builder builder)中的方法,我在其中…

火狐 增强查找工具栏_在“提示”框中:简单的IE至Firefox同步,轻松的Windows工具栏和识别USB电缆...

火狐 增强查找工具栏() Every week we tip into our mail bag and share great tips from your fellow readers. This week we’re looking at an easy way to sync your bookmarks between IE and Firefox, using simple Windows toolbars, and a clever way to ID USB cables…

Input Director使用一个键盘和鼠标即可控制多台Windows计算机

The problem is having two or more PC’s and having to go back and forth between workstation. Input Director solves the problem by allowing you to control multiple Windows systems with only one keyboard and mouse on the Master PC. 问题是拥有两台或更多台PC…

excel导入csv文件_如何将包含以0开头的列的CSV文件导入Excel

excel导入csv文件Microsoft Excel will automatically convert data columns into the format that it thinks is best when opening comma-separated data files. For those of us that don’t want our data changed, we can change that behavior. Microsoft Excel将在打开…

android 9.0新ui,SystemUI分析(Android9.0)

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;一、SystemUI组成SystemUI是Android的系统界面&#xff0c;包括状态栏statusbar、锁屏keyboard、任务列表recents等等&#xff0c;都继承于SystemUI这个类&#xf…

chromebook刷机_如何为不支持Chrome操作系统的网站欺骗Chromebook用户代理

chromebook刷机Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

cocos android-1,cocos2dx在windows下开发,编译到android上(1)

转自&#xff1a;http://www.2cto.com/kf/201205/130697.html下面我给大家介绍下&#xff0c;用vs2010开发cocos2dx&#xff0c;然后如何使其编译到android上。步骤如下&#xff1a;1、必要条件&#xff0c;你的eclipse能把代码编译到安卓手机或虚拟机上&#xff0c;如果这一步…

中药ppi网络图太杂乱_太杂乱了吗? 这是您的iPhone,iPad,Android或台式机的15张简约壁纸...

中药ppi网络图太杂乱Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墙纸图像在iPhon…

初学者java学习计划_初学者:计划在Windows 7 Media Center中录制直播电视的时间

初学者java学习计划If you’re a new user to Windows 7 Media Center you know it can act as a DVR and pause or record Live TV. You can set up a schedule for it to record your favorite TV programs as well. 如果您是Windows 7 Media Center的新用户&#xff0c;则知…

如何在Office 2007中查看关于对话框和版本信息

One of our favorite readers wrote in today asking how to tell if his Word 2007 installation was running Service Pack 1, since he couldn’t find the About dialog, which got me thinking… I bet most people don’t know where it is! 我们最喜欢的一位读者今天写信…

windows全局热键_在Windows中创建快捷方式或热键以清除剪贴板

windows全局热键Have you ever copied something to the clipboard that you don’t want to leave there in case somebody else is going to use your computer? Sure, you can copy something else to the clipboard real quick, but can’t you just make a shortcut or h…

获取outlook 会议_如何仅在Microsoft Outlook中仅获取您关注的电子邮件的通知

获取outlook 会议Some emails are more important than others. Rather than getting alerts every time an email arrives, configure Microsoft Outlook to only alert you when the important stuff hits your inbox, rather than any old email that can wait until you ch…

jq html 多一个引号,为什么jQuery模板会为某些字符串添加双引号

背景我正在使用jQuery模板,ASP.Net MVC Razor视图和Twitter.问题使用带有一些字符串的jQuery模板会自动导致这些字符串被包含在“细节我创建了一个如下所示的jQuery模板&#xff1a;before ${text.parseUserName().parseHashTag()} after${created_at}${prettyDate(created_at)…

从Windows计算机上完全删除iTunes和其他Apple软件

If you are giving up on iTunes for another music player, uninstalling it completely can be a hassle. Here we show you how to completely remove all traces of it including QuickTime, iTunes Helper, Bonjour…all of it. 如果您在iTunes上放弃了其他音乐播放器&…

html仿微信滑动删除,使用Vue实现移动端左滑删除效果附源码

左滑删除在移动端是很常见的一种操作&#xff0c;常见于删除购物车中的商品&#xff0c;删除收藏夹中文章等等场景。我们只需要手指按住要删除的对象&#xff0c;然后轻轻向左滑动&#xff0c;便会出现删除按钮&#xff0c;然后点击删除按钮即可删除对象。点击下载源码今天我给…