美文网首页
RecyclerView/Adapter调用notifyItem

RecyclerView/Adapter调用notifyItem

作者: maiduoduo | 来源:发表于2021-05-19 13:41 被阅读0次

RecyclerView调用notifyItemChanged闪烁问题相信很多人都遇到过。
那是因为recyclerView默认设置的动画DefaultItemAnimator造成的,
DefaultItemAnimator继承自SimpleItemAnimator,里面有个方法是
只要设置为false,就可以不显示动画了,也就解决了闪烁问题。

一行代码搞定
  ((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
查看源码
/**
    * Sets whether this ItemAnimator supports animations of item change events.
    * If you set this property to false, actions on the data set which change the
    * contents of items will not be animated. What those animations do is left
    * up to the discretion of the ItemAnimator subclass, in its
    * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation.
    * The value of this property is true by default.
    *
    * @param supportsChangeAnimations true if change animations are supported by
    *                                 this ItemAnimator, false otherwise. If the property is false,
    *                                 the ItemAnimator
    *                                 will not receive a call to
    *                                 {@link #animateChange(ViewHolder, ViewHolder, int, int, int,
    *                                 int)} when changes occur.
    * @see Adapter#notifyItemChanged(int)
    * @see Adapter#notifyItemRangeChanged(int, int)
    */
   public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {
       mSupportsChangeAnimations = supportsChangeAnimations;
   }

相关文章

网友评论

      本文标题:RecyclerView/Adapter调用notifyItem

      本文链接:https://www.haomeiwen.com/subject/zvdpjltx.html