在 RecyclerView.Adapter 中覆盖 notifyItemChanged 的​​动画 [英] Override animation for notifyItemChanged in RecyclerView.Adapter

查看:19
本文介绍了在 RecyclerView.Adapter 中覆盖 notifyItemChanged 的​​动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我有一个带适配器的 RecyclerView,一切都很好.ArrayList 数据集中的项目会定期更新.因此,项目及其元素以及它们在列表中的位置都会发生变化.这是通过简单的排序和手动调用这些方法来实现的:

Well, I have a RecyclerView with an adapter and everything works great. The items in the ArrayList dataset are being updated periodically. So the items and their elements as well as their position in the list change. This is achieved by simple sorting and manually calling these methods, whenever things happen:

// swapping two items
Collections.swap(items, i, j);
itemsAdapter.notifyItemMoved(i, j);

// adding a new one
itemAdapter.notifyItemInserted(items.size());

// when updating valus
itemAdapter.notifyItemChanged(i);

后者,是我痛苦的原因.每次更新项目时,都会触发一点闪烁"动画.

The latter of which, is the cause of my misery. Every time an item is updated, a little "blink" animation is triggered.

我为此找到了几个解决方案:

I found a couple of solutions for this:

// disabling all animations
recyclerView.getItemAnimator().setSupportsChangeAnimations(false);

// or

// setting the animation duration to zero,
recyclerView.getItemAnimator().setChangeDuration(0);

但是当项目移动(被交换)时,这两个都会杀死动画.我只想覆盖一个动画并保留所有这些魔法.有没有办法做到这一点?如果它覆盖了 ItemAnimator,有没有人有一个简单的例子?

But both of these kill the animations when items move (being swapped). I just want to override the one animation and keep all of this magic. Is there a way of doing this? And if it's overriding ItemAnimator, does anyone have a simple example?

提前致谢!

推荐答案

是的,我做到了.

首先获取DefaultItemAnimator的源码.获取代码并在您的项目中创建一个名为 MyItemAnimator 的类.然后,将 ItemAnimator 设置为修改后的 MyItemAnimator 的新实例,如下所示:

First, get the source code of DefaultItemAnimator. Take the code and create a class named MyItemAnimator in your project. Then, set the ItemAnimator to a new instance of your modified MyItemAnimator, like so:

recyclerView.setItemAnimator(new MyItemAnimator());

现在,进入新的类源代码并找到方法

Now, go in the new classes source code and locate the method

animateChangeImpl(final ChangeInfo changeInfo) { ... }

我们只需要定位改变 alpha 值的方法调用.找到以下两行并删除 .alpha(0) 和 .alpha(1)

We simply have to locate the method calls changing alpha values. Find the following two lines and remove the .alpha(0) and .alpha(1)

oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() { ... }
newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()).alpha(1).setListener(new VpaListenerAdapter() { ... }

喜欢

oldViewAnim.setListener(new VpaListenerAdapter() { ... }
newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()).setListener(new VpaListenerAdapter() { ... }

这篇关于在 RecyclerView.Adapter 中覆盖 notifyItemChanged 的​​动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆