RecyclerView 预测项目动画不起作用(出现) [英] RecyclerView predictive item animations not working (APPEARING)

查看:32
本文介绍了RecyclerView 预测项目动画不起作用(出现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 GridLayoutManager 的 RecyclerView(支持 lib v7).我更新适配器并使notifyItemMoved.如果项目源位置在屏幕上并且目标位置也在屏幕上 - 播放移动动画".如果项目位于屏幕外的位置(位置 1000,或任何其他远离视口的位置)并移动到视口中,它将显示为动画淡入淡出".

I have a RecyclerView with GridLayoutManager (support lib v7). I update adapter and make notifyItemMoved. If the item source position is on the screen and the destination position is on the screen as well - "move animation" is played. In case if the item was at position out of the screen(position 1000, or any other far from viewport) and moved into viewport it will appear with "fade in animation".

看起来预测性项目动画不起作用,尽管 supportPredictiveItemAnimations() 返回 true.难道我做错了什么?我应该覆盖一些方法来启用它吗?

Looks like predictive item animations are not working despite on the fact supportsPredictiveItemAnimations() returns true. Am I doing something wrong? Should I override some methods to enable it?

我正在阅读RecyclerView的源代码,在dispatchLayout方法的javadoc中是这样写的:

I was reading the source code of RecyclerView, and in javadoc of the dispatchLayout method is written like:

  • PERSISTENT 视图被移动 ({@link ItemAnimator#animateMove(ViewHolder, int, int, int, int)})
  • REMOVED 视图被移除 ({@link ItemAnimator#animateRemove(ViewHolder)})
  • 添加了添加的视图 ({@link ItemAnimator#animateAdd(ViewHolder)})
  • 消失的视图被移出屏幕
  • 出现的视图在屏幕上移动

尽管如此,ItemAnimator 不区分 ADDED 和 APPEARING.是否有可能修复预测动画或至少使 APPEARING 动画看起来像从屏幕动画外部移动"而保留 ADDED 动画?

Nevertheless ItemAnimator doesn't distinguish ADDED and APPEARING. Is it possible fix predictife animations or at least make APPEARING animation look like "move from outside of the screen animation" lefting ADDED animation as is?

推荐答案

我遇到了同样的问题.检查您是否设置了 ItemAnimator.预测动画依赖于它,所以如果你打算使用 TransitionManager 代替,那么创建空的 ItemAnimator 来启用预测动画.

I had same problem. Check if you have set ItemAnimator. Predictive animation depends on it, so if you are going to use TransitionManager instead, then create empty ItemAnimator to enable predictive animations.

您可以查看源代码(LinearLayoutManager: group: 'com.android.support', name: 'recyclerview-v7', version: '27.0.2').

You can check source code (LinearLayoutManager: group: 'com.android.support', name: 'recyclerview-v7', version: '27.0.2').

第 700-703 行:

Lines 700-703:

if (!state.willRunPredictiveAnimations() ||  getChildCount() == 0 || state.isPreLayout()
            || !supportsPredictiveItemAnimations()) {
        return;
    }

state.willRunPredictiveAnimations() - 如果 ItemAnimator 为 null,将返回 false.

state.willRunPredictiveAnimations() - will return false if ItemAnimator is null.

EmptyItemAnimator.java

EmptyItemAnimator.java

public class EmptyItemAnimator extends RecyclerView.ItemAnimator {
@Override
public boolean isRunning() {
    return false;
}

@Override
public void endAnimations() {}

@Override
public boolean animatePersistence(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
    return false;
}

@Override
public void endAnimation(RecyclerView.ViewHolder item) {}

@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 boolean animateAppearance(@NonNull RecyclerView.ViewHolder viewHolder, @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
    return false;
}

@Override
public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) {
    return false;
}}

现在你可以试试:

TransitionManager.beginDelayedTransition(recyclerView);
recyclerViewAdapter.notifyItem... //add, remove, change etc.

这篇关于RecyclerView 预测项目动画不起作用(出现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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