在 notifyItemRangeInserted() 上禁用动画

Disable animation on notifyItemRangeInserted()

我正在使用 RecyclerView。将项目添加到 RecyclerView 后,我需要调用:

notifyItemRangeInserted(int positionStart, int itemCount);

但是,这显示了一种 "slide down" 动画。有什么方法可以禁用此动画吗?

谢谢。

尝试清除 RecyclerView 项目动画

recyclerView.setItemAnimator(null);

如果需要,您可以在之后重新启用动画。

recyclerView.setItemAnimator(null);
notifyItemRangeInserted(int positionStart, int itemCount);
recyclerView.setItemAnimator(new DefaultItemAnimator());

您也可以在 xml 布局文件中使用数据绑定,如下所示:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemAnimator="@{null}" />

这是可能的,因为 RecyclerView 有一个名为 setItemAnimator!

的 public 函数