我正在使用 RecyclerView。将项目添加到 RecyclerView 后,我需要调用:
notifyItemRangeInserted(int positionStart, int itemCount);
然而,这显示了一种“向下滑动”的动画。有没有办法可以禁用此动画?
谢谢。
尝试清除
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
! 的公共函数
我正在使用 ListAdapter,Matthew 的解决方案对我不起作用,当我重新启用动画时,它会激活动画。如果有人需要,我会使用它(Kotlin):
recyclerView.itemAnimator = null
adapter.submitList(items.toList()){
recyclerView.itemAnimator = DefaultItemAnimator()
}