博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RecyclerView notifyItem闪屏问题
阅读量:2175 次
发布时间:2019-05-01

本文共 2705 字,大约阅读时间需要 9 分钟。

原文链接

RecyclerView刷新方法

操作内容

ListView的getView方法的渲染数据部分的代码相当于onBindViewHolder(),如果调用adapter.notifyDataSetChanged()方法,会重新调用onBindViewHolder()方法。

其他刷新方法

除了adapter.notifyDataSetChanged()这个方法之外,新的Adapter还提供了其他的方法,如下:

//刷新所有public final void notifyDataSetChanged();//position数据发生了改变,那调用这个方法,就会回调对应position的onBindViewHolder()方法了public final void notifyItemChanged(int position);//刷新从positionStart开始itemCount数量的item了(这里的刷新指回调onBindViewHolder()方法)public final void notifyItemRangeChanged(int positionStart, int itemCount);//在第position位置被插入了一条数据的时候可以使用这个方法刷新,注意这个方法调用后会有插入的动画,这个动画可以使用默认的,也可以自己定义public final void notifyItemInserted(int position);//从fromPosition移动到toPosition为止的时候可以使用这个方法刷新public final void notifyItemMoved(int fromPosition, int toPosition);//批量添加public final void notifyItemRangeInserted(int positionStart, int itemCount);//第position个被删除的时候刷新,同样会有动画public final void notifyItemRemoved(int position);//批量删除public final void notifyItemRangeRemoved(int positionStart, int itemCount);

闪屏问题

问题描述

RecyclerView做了一个notifyItemChanged()的操作,功能都顺利实现,问题当前Item闪烁,QA甚至为此提了Bug。

问题原因

闪烁主要由于RecyclerView使用的默认的动画导致的,所以解决的方法就是修改默认的动画。

问题解决

最简单方法

DefaultItemAnimator继承自SimpleItemAnimator,里面有个方法是:    /**     * 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.     *     */    public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {        mSupportsChangeAnimations = supportsChangeAnimations;    }

只要设置为false,就可以不显示动画了,也就解决了闪烁问题。 关键代码:

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

修改默认的动画方法

//1.定义动画类public class NoAlphaItemAnimator extends RecyclerView.ItemAnimator {
}//2.将DefaultItemAnimator类里的代码全部copy到自己写的动画类中,然后做一些修改。//3.首先找到private void animateChangeImpl(final ChangeInfo changeInfo) {}方法。//4.找到方法里这两句代码:4.1 去掉alpha(0)oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() {...}).start();oldViewAnim.setListener(new VpaListenerAdapter() {...}).start();4.2 去掉alpha(1)newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()). alpha(1).setListener(new VpaListenerAdapter() {...}).start();newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()). setListener(new VpaListenerAdapter() {...}).start();//5.最后使用修改后的动画recyclerView.setItemAnimator(new NoAlphaItemAnimator());

转载地址:http://ekukb.baihongyu.com/

你可能感兴趣的文章
TCP为什么是三次握手,为什么不是两次或者四次 && TCP四次挥手
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
逆序对的数量(递归+归并思想)
查看>>
数的范围(二分查找上下界)
查看>>
算法导论阅读顺序
查看>>
Windows程序设计:直线绘制
查看>>
linux之CentOS下文件解压方式
查看>>
Django字段的创建并连接MYSQL
查看>>
div标签布局的使用
查看>>
HTML中表格的使用
查看>>
(模板 重要)Tarjan算法解决LCA问题(PAT 1151 LCA in a Binary Tree)
查看>>
(PAT 1154) Vertex Coloring (图的广度优先遍历)
查看>>