在notifyItemRangeInserted()上禁用动画

d84*_*619 13 android android-animation android-recyclerview

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

notifyItemRangeInserted(int positionStart, int itemCount);
Run Code Online (Sandbox Code Playgroud)

但是,这显示了一种"向下滑动"动画.有没有办法可以禁用这个动画?

谢谢.

Mat*_*rer 34

尝试清除RecyclerView项目动画师

recyclerView.setItemAnimator(null);
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以重新启用动画.

recyclerView.setItemAnimator(null);
notifyItemRangeInserted(int positionStart, int itemCount);
recyclerView.setItemAnimator(new DefaultItemAnimator());
Run Code Online (Sandbox Code Playgroud)


小智 8

您还可以使用 xml 布局文件中的数据绑定来完成此操作,如下所示:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemAnimator="@{null}" />
Run Code Online (Sandbox Code Playgroud)

这是可能的,因为 RecyclerView 有一个名为setItemAnimator!的公共函数。