RecyclerView notifyItem闪屏问题
来源:互联网 发布:套马杆网络歌手演唱 编辑:程序博客网 时间:2024/06/06 02:00
原文链接
http://blog.csdn.net/chenliguan/article/details/52809758
http://www.jianshu.com/p/654dac931667
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://stackoverflow.com/questions/31897469/override-animation-for-notifyitemchanged-in-recyclerview-adapter
0 0
- RecyclerView notifyItem闪屏问题
- Android进阶之解决RecyclerView notifyItem闪屏问题
- Android进阶之解决RecyclerView notifyItem闪屏问题
- Android进阶之解决RecyclerView notifyItem闪屏问题
- RecyclerView notifyItem闪烁的问题
- RecyclerView notifyItem闪烁的问题
- RecyclerView闪屏问题
- RecyclerView刷新数据源闪屏问题
- android 解决RecyclerView notifyDataSetChanged刷新闪屏问题(图片刷新)
- RecyclerView缓存的问题
- recyclerview出现的问题
- ScrollView嵌套recyclerView问题
- Recyclerview.getLayoutPosition()问题
- RecyclerView注意问题--待续
- RecyclerView 报错问题
- recyclerView导入问题
- RecyclerView遇到的问题
- RecyclerView居中问题
- 【树莓派开发-Linux】sudo apt-get update
- 轻松自制flyme悬浮球
- Team Queue
- Leetcode||30.Substring with Concatenation of All Words
- |题目分类|动态规划
- RecyclerView notifyItem闪屏问题
- BP神经网络
- POJ - 3579(二分法找中位数)
- Win7下安装Oracle11g
- Struts框架搭建
- Eclipse中文乱码解决方法总结
- JAVA带你一步一步实现微信公众号开发(一)
- 【Dongle】【Java】基础(五)之包及引用
- Ubuntu16.04 LTS安装Android studio