- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 RecycleView,其中显示了一个项目列表。我像这样为 RecyclerView 指定默认动画师:
recyclerView.setItemAnimator( new DefaultItemAnimator() );
一切正常,但我想使用我自己的自定义动画来添加/删除/更新列表中的元素。
我像这样定义了一个自定义动画师类:
public class MyAnimator extends RecyclerView.ItemAnimator {
@Override
public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) {
return false;
}
@Override
public boolean animateAppearance(@NonNull RecyclerView.ViewHolder viewHolder, @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
return false;
}
@Override
public boolean animatePersistence(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
return false;
}
@Override
public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder, @NonNull RecyclerView.ViewHolder newHolder, @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
return false;
}
@Override
public void runPendingAnimations() {
}
@Override
public void endAnimation(RecyclerView.ViewHolder item) {
}
@Override
public void endAnimations() {
}
@Override
public boolean isRunning() {
return false;
}
}
并按照我对 DefaultItemAnimator 所做的相同方式进行设置。动画不再播放了,所以我猜它起作用了,但问题是项目有时会堆叠在一起,当我移除所有项目时,还有一些仍然存在,所以我想我遗漏了一些东西。
据我了解,animateDisappearance 是一种在项目从列表中删除时调用的方法。如果我返回 false,就我所知,它应该只是跳过动画,对吗?
我走在正确的轨道上吗?当我在 github 上寻找这方面的示例时,结果很少,总的来说我似乎找不到任何基本代码示例如何执行此操作,而我找到的都是数千行代码。
如何在不使用任何外部库的情况下简单地用自己的动画覆盖默认的添加/删除动画?谢谢!
编辑:
我能够通过以下方式覆盖默认动画:
recyclerView.setItemAnimator(new DefaultItemAnimator() {
@Override
public boolean animateRemove(RecyclerView.ViewHolder holder) {
holder.itemView.clearAnimation();
final RecyclerView.ViewHolder h = holder;
holder.itemView.animate()
.alpha(0)
.setInterpolator(new AccelerateInterpolator(2.f))
.setDuration(1350)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
dispatchRemoveFinished(h);
}
})
.start();
//
return false;
}
} );
动画完美运行,但出于某种原因,似乎立即触发了“dispatchRemoveFinished”,因此它们不会在动画后调整其余元素,而是在 View 被移除后立即执行。有什么办法可以解决这个问题吗?
最佳答案
在实现您的RecyclerView.ItemAnimator
时,您必须遵循一些规则,否则 RecyclerView 状态会变得一团糟:
所有返回 false 的空方法必须至少调用 dispatchAnimationFinished(viewHolder)
以及清除动画状态。
如果这些方法要启动动画,您应该dispatchAnimationStarted(viewHolder)
,存储动画请求并返回 true 以调用 runPendingAnimations()
,其中动画应该真正开始。
您需要跟踪正在进行的动画以便能够正确取消它们,您也会收到对已经在动画中的项目的请求。
这是一个示例 ItemAnimator
,它只对移除和移动进行动画处理。请注意充当动画数据持有者和动画状态监听器的内部类:
public class RecAnimator extends RecyclerView.ItemAnimator {
private final static String TAG = "RecAnimator";
private final static int ANIMATION_TYPE_DISAPPEAR = 1;
private final static int ANIMATION_TYPE_MOVE = 2;
// must keep track of all pending/ongoing animations.
private final ArrayList<AnimInfo> pending = new ArrayList<>();
private final HashMap<RecyclerView.ViewHolder, AnimInfo> disappearances = new HashMap<>();
private final HashMap<RecyclerView.ViewHolder, AnimInfo> persistences = new HashMap<>();
@Override
public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) {
pending.add(new AnimInfo(viewHolder, ANIMATION_TYPE_DISAPPEAR, 0));
dispatchAnimationStarted(viewHolder);
// new pending animation added, return true to indicate we want a call to runPendingAnimations()
return true;
}
@Override
public boolean animateAppearance(@NonNull RecyclerView.ViewHolder viewHolder, @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
dispatchAnimationFinished(viewHolder);
return false;
}
@Override
public boolean animatePersistence(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
if (preLayoutInfo.top != postLayoutInfo.top) {
// required movement
int topDiff = preLayoutInfo.top - postLayoutInfo.top;
AnimInfo per = persistences.get(viewHolder);
if(per != null && per.isRunning) {
// there is already an ongoing animation - update it instead
per.top = per.holder.itemView.getTranslationY() + topDiff;
per.start();
// discard this animatePersistence call
dispatchAnimationFinished(viewHolder);
return false;
}
pending.add(new AnimInfo(viewHolder, ANIMATION_TYPE_MOVE, topDiff));
dispatchAnimationStarted(viewHolder);
// new pending animation added, return true to indicate we want a call to runPendingAnimations()
return true;
}
dispatchAnimationFinished(viewHolder);
return false;
}
@Override
public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder, @NonNull RecyclerView.ViewHolder newHolder, @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
dispatchAnimationFinished(oldHolder);
dispatchAnimationFinished(newHolder);
return false;
}
@Override
public void runPendingAnimations() {
for (AnimInfo ai: pending) {
ai.start();
}
pending.clear();
}
@Override
public void endAnimation(RecyclerView.ViewHolder item) {
AnimInfo ai = disappearances.get(item);
if (ai != null && ai.isRunning) {
ai.holder.itemView.animate().cancel();
}
ai = persistences.get(item);
if (ai != null && ai.isRunning) {
ai.holder.itemView.animate().cancel();
}
}
@Override
public void endAnimations() {
for (AnimInfo ai: disappearances.values())
if (ai.isRunning)
ai.holder.itemView.animate().cancel();
for (AnimInfo ai: persistences.values())
if (ai.isRunning)
ai.holder.itemView.animate().cancel();
}
@Override
public boolean isRunning() {
return (!pending.isEmpty()
|| !disappearances.isEmpty()
|| !persistences.isEmpty());
}
/**
* This is container for each animation. It's also cancel/end listener for them.
* */
private final class AnimInfo implements Animator.AnimatorListener {
private final RecyclerView.ViewHolder holder;
private final int animationType;
private float top;
private boolean isRunning = false;
private AnimInfo(RecyclerView.ViewHolder holder, int animationType, float top) {
this.holder = holder;
this.animationType = animationType;
this.top = top;
}
void start(){
View itemView = holder.itemView;
itemView.animate().setListener(this);
switch (animationType) {
case ANIMATION_TYPE_DISAPPEAR:
itemView.setPivotY(0f);
itemView.animate().scaleX(0f).scaleY(0f).setDuration(getRemoveDuration());
disappearances.put(holder, this); // must keep track of all animations
break;
case ANIMATION_TYPE_MOVE:
itemView.setTranslationY(top);
itemView.animate().translationY(0f).setDuration(getMoveDuration());
persistences.put(holder, this); // must keep track of all animations
break;
}
isRunning = true;
}
private void resetViewHolderState(){
// reset state as if no animation was ran
switch (animationType) {
case ANIMATION_TYPE_DISAPPEAR:
holder.itemView.setScaleX(1f);
holder.itemView.setScaleY(1f);
break;
case ANIMATION_TYPE_MOVE:
holder.itemView.setTranslationY(0f);
break;
}
}
@Override
public void onAnimationEnd(Animator animation) {
switch (animationType) {
case ANIMATION_TYPE_DISAPPEAR:
disappearances.remove(holder);
break;
case ANIMATION_TYPE_MOVE:
persistences.remove(holder);
break;
}
resetViewHolderState();
holder.itemView.animate().setListener(null); // clear listener
dispatchAnimationFinished(holder);
if (!isRunning())
dispatchAnimationsFinished();
isRunning = false;
}
@Override
public void onAnimationCancel(Animator animation) {
// jump to end state
switch (animationType) {
case ANIMATION_TYPE_DISAPPEAR:
holder.itemView.setScaleX(0f);
holder.itemView.setScaleY(0f);
break;
case ANIMATION_TYPE_MOVE:
holder.itemView.setTranslationY(0f);
break;
}
}
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}
}
您还可以覆盖 SimpleItemAnimator为您将 animate...
方法解析为 animateMove
、animateRemove
等的类。
关于android - 正确覆盖 RecyclerView 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696530/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!