(转)RecyclerView调用notifyItemChanged闪烁问题

一行代码搞定:

((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

原理:

RecyclerView调用notifyItemChanged闪烁问题相信很多人都遇到过。
那是因为recyclerView默认设置的动画DefaultItemAnimator造成的,
DefaultItemAnimator继承自SimpleItemAnimator,里面有个方法是
只要设置为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;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,327评论 25 709
  • RecyclerView 源码分析 本文原创,转载请注明出处。欢迎关注我的 简书 ,关注我的专题 Android ...
    MeloDev阅读 13,415评论 6 49
  • 这篇文章分三个部分,简单跟大家讲一下 RecyclerView 的常用方法与奇葩用法;工作原理与ListView比...
    LucasAdam阅读 9,924评论 0 27
  • 原来,真的深夜才是真正吐露心声的时候。谢谢好朋友好哥们好闺蜜诙谐幽默爱的引导和鼓励,我会善待自己,调整好心态,做回...
    张家的阅读 1,007评论 0 1
  • App Programming Guide for iOS翻译 https://developer.apple.c...
    鋼鉄侠阅读 4,803评论 0 1