RecyclerView notifyItem闪屏问题
生活随笔
收集整理的這篇文章主要介紹了
RecyclerView notifyItem闪屏问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文鏈接
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
總結
以上是生活随笔為你收集整理的RecyclerView notifyItem闪屏问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio 权威教程
- 下一篇: Xml转换成view的原理