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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Recyclerview 特别好用的局部刷新item方法

發布時間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Recyclerview 特别好用的局部刷新item方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一般列表刷新,會使用notifyDataChanged,會使列表整個刷新,雖然達到了效果,但是不美觀。所以,如果可以做到只刷新需要刷新的item就好了。這里就有個特好用的方法,分享給大家。

/*** 更新某一個item數據* @param nearbyNewAnchorInfo*/public void updateNearbyAndNewAnchorData(RecyclerView recyclerView, NearByNewAnchorInfo nearbyNewAnchorInfo){int headerCount = ((HeaderAndFooterWrapper) (recyclerView.getAdapter())).getHeadersCount();//極致的item刷新優化RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(nearbyNewAnchorInfo.Index+headerCount);if (viewHolder != null && viewHolder instanceof NearbyNewViewHolder) {NearbyNewViewHolder itemHolder = (NearbyNewViewHolder) viewHolder;itemHolder.nearbyView.updateData(nearbyNewAnchorInfo.StarLiveRooms,true);itemHolder.newView.updateData(nearbyNewAnchorInfo.NearLiveRooms,true);}}

主要使用到的方法: public ViewHolder findViewHolderForAdapterPosition(int position)

/**
* Return the ViewHolder for the item in the given position of the data set. Unlike
* {@link #findViewHolderForLayoutPosition(int)} this method takes into account any pending
* adapter changes that may not be reflected to the layout yet. On the other hand, if
* {@link Adapter#notifyDataSetChanged()} has been called but the new layout has not been
* calculated yet, this method will return null since the new positions of views
* are unknown until the layout is calculated.
*


* This method checks only the children of RecyclerView. If the item at the given
* position is not laid out, it will not create a new one.
*


* When the ItemAnimator is running a change animation, there might be 2 ViewHolders
* representing the same Item. In this case, the updated ViewHolder will be returned.
*
* @param position The position of the item in the data set of the adapter
* @return The ViewHolder at position or null if there is no such item
*/

需要注意的是,這里的position指的是item在列表中的位置,而不是item數據在列表數據的位置。所以需要將列表中的頭部視圖計算進去,才可以獲取到真正該位置上的item。

總結

以上是生活随笔為你收集整理的Recyclerview 特别好用的局部刷新item方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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