日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RecyclerView notifyItem闪屏问题

發布時間:2025/4/16 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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闪屏问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。