【问题标题】:how to Use RecyclerView.ItemAnimator With RecyclerView如何使用 RecyclerView.ItemAnimator 和 RecyclerView
【发布时间】:2020-03-17 04:25:50
【问题描述】:

我是 Android 开发的初学者。当我搜索 RecyclerView 动画时,我发现了 RecyclerView.ItemAnimator,但我找不到任何关于如何使用它的教程。我读过这个How to use ItemAnimator in a RecyclerView?。但是,我仍然没有得到任何东西。

我找到了文档。

默认情况下,RecyclerView 使用 DefaultItemAnimator。

如果我使用 RecyclerView,可以从 DefaultItemAnimator 期待什么类型的动画?

我用过LayoutAnimation,什么时候用RecyclerView.ItemAnimator?

【问题讨论】:

  • 在更新您的RecycleView 时,默认项目动画器会起作用。假设您删除、添加或更新。例如,删除时 Gmail 或 Messages 中的动画。
  • 如果我有一个静态列表,它不起作用?
  • 你可以在加载RecyclerView时生效。
  • 抱歉。我英语也很弱。你能给我更多的信息吗?你的意思是什么?
  • 好的。一个简单但直观的方式是参考这个库github.com/wasabeef/recyclerview-animators。这就是项目动画师的工作方式。

标签: android android-recyclerview


【解决方案1】:

您可以使用任何类型的动画来为您的回收站视图项设置动画

构建以下行为所需的步骤

  1. 首先在res/anim/ 中创建文件item_animation_fall_down.xml 并添加以下内容:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:duration="@integer/anim_duration_medium">
    
        <translate
            android:fromYDelta="-20%"
            android:toYDelta="0"
            android:interpolator="@android:anim/decelerate_interpolator"
            />
    
        <alpha
            android:fromAlpha="0"
            android:toAlpha="1"
            android:interpolator="@android:anim/decelerate_interpolator"
            />
    
        <scale
            android:fromXScale="105%"
            android:fromYScale="105%"
            android:toXScale="100%"
            android:toYScale="100%"
            android:pivotX="50%"
            android:pivotY="50%"
            android:interpolator="@android:anim/decelerate_interpolator"
            />
    
    </set>
    
  2. 定义布局动画

项目动画完成后,是时候定义布局动画了,它将项目动画应用于布局中的每个子项。在res/anim/ 中创建一个名为layout_animation_fall_down.xml 的新文件并添加以下内容:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_animation_fall_down"
    android:delay="15%"
    android:animationOrder="normal"
    />
  1. 应用布局动画

LayoutAnimation 既可以通过编程方式应用,也可以在 XML 中应用。

JAVA

int resId = R.anim.layout_animation_fall_down;
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ctx, resId);
recyclerview.setLayoutAnimation(animation);

XML

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"                                        
    android:layoutAnimation="@anim/layout_animation_fall_down"
    />

欲了解更多信息,请访问here

【讨论】:

  • 那么ItemAnimator有什么用呢?我应该什么时候使用?
  • 你可以使用ItemAnimator来应用默认动画或创建一个类然后扩展ItemAnimator并应用你想要的动画,毕竟将创建的类设置为默认动画
【解决方案2】:

您希望为 recyclerview 的行设置动画。使用下面的函数

 public void setFadeAnimation(View view) {
    AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(1000);
    view.startAnimation(anim);
}

现在转到 Adapter 类并在 onBindViewHolder 方法中像这样调用这个函数

setFadeAnimation(myViewHolder.itemView);

它将为您的行设置动画

【讨论】:

  • 感谢您的回答。但是我想知道ItemAnimator有什么用?我应该什么时候使用?
猜你喜欢
  • 2015-10-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多