【问题标题】:Disable animation on notifyItemRangeInserted()在 notifyItemRangeInserted() 上禁用动画
【发布时间】:2017-06-01 00:30:52
【问题描述】:

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

notifyItemRangeInserted(int positionStart, int itemCount);

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

谢谢。

【问题讨论】:

    标签: android android-recyclerview android-animation


    【解决方案1】:

    尝试清除RecyclerView item animator

    recyclerView.setItemAnimator(null);
    

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

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

    【讨论】:

      【解决方案2】:

      您也可以像这样在 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 的公共函数!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-24
        • 2021-05-07
        • 2023-03-13
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        相关资源
        最近更新 更多