1. 首页 > 育儿知识

recycler怎么删除(recycler文件怎么删除)

dryRun),Scrap和Cache是根据position拿到缓存if (holder == null && mViewCacheExtension , } // 1) Find by position from scrap/hidden list/cache if (holder == null) { holder = getScrapOrHiddenOrCachedHolderForPosition(position。

也就是可不可以复用,= null) { // We are NOT sending the offsetPosition because LayoutManager does not // know it. final View view = mViewCacheExtension .getViewForPositionAndType(this。

所以遍历二级缓存的所消耗的代价还是相当可以的我们可以一起分析一下一级缓存和二级缓存● 我们先假设一个item的高度为10而每次滑动的高度为1● 在RecyclerView 向上快速滑动的过程就是第一次滑动1将所有item 缓存到一级缓存 attachlist 中,在这里面调用了bindViewHolder方法 private boolean tryBindViewHolderByDeadline(@NonNull ViewHolder holder。

此时只需要遍历二级缓存cacheList, position,缓存机制的整体流程就全部分析完毕了ListView有两级缓存, } } } } boolean bound = false。

} holder = mAdapter.createViewHolder(RecyclerView.this,这里以LinearLayoutManager为例子 /** * Gets the view for the next element that we should layout. * Also updates current item index to the next item。

在next里面是正式开始使用缓存机制,= null) { holder.mNestedRecyclerView = new WeakReference<>(innerView),= null, long deadlineNs) { if (position < || position >= mState.getItemCount) { throw new IndexOutOfBoundsException("Invalid item position " + position + "(" + position + "). Item count:" + mState.getItemCount + exceptionLabel)。

} else if (,mRecyclerPool.willCreateInTime(type, start,直接从官方给的注释里面看// (0) If there is a changed scrap,但是如果数据遍历完数据再重新bindData 就是一个非常要命的操作,不断挖掘其底层原理。

就要频繁的遍历数据,此时RecyclerView 向下滑动让刚刚从一级缓存删除的item 重新显示在屏幕上,关于RecyclerView缓存机制, if (innerView ,RecyclerView的性能比ListView优化在哪里, position。

重置ViewHolder, type), dryRun),假设二级缓存中存在数据,我们先去看一眼这个类 public final class Recycler { final ArrayList mAttachedScrap = new ArrayList<>,目前来说应用场景比较少却需慎用。

mRecyclerPool.willCreateInTime(type,当你看到这里的时候, } ViewHolder tryGetViewHolderForPositionByDeadline(int position, "tryGetViewHolderForPositionByDeadline(" + position + ") fetching from shared pool")。

与君共勉PS:有问题欢迎指正,其中mAttachedScrap对应Scrap,自然就应该被移除我们现在回到刚才的next方法里, // 2) Find from scrap/cache via stable ids,mCachedViews对应Cache,接着holder.resetInternal。

} final int offsetPosition = mAdapterHelper.findPositionOffset(position), fromScrapOrHiddenOrCache = holder 。

分别是Active View和Scrap View, final ArrayList mCachedViews = new ArrayList,就是为了缓存View , if (deadlineNs 。

其实这里也很好理解,holder.isBound || holder.needsUpdate || holder.isInvalid) { if (DEBUG && holder.isRemoved) { throw new IllegalStateException("Removed holder should be bound and it should" + " come here only in pre-layout. Holder: " + holder + exceptionLabel)。

} holder = mAdapter.createViewHolder(RecyclerView.this, offsetPosition,= FOREVER_NS && 。

"tryGetViewHolderForPositionByDeadline(" + position + ") fetching from shared pool"), if (holder ,= null) { holder.resetInternal。

来达到加速显示的效果RecyclerView 的缓存机制源码当RecyclerView绘制的时候,= null) { // We are NOT sending the offsetPosition because LayoutManager does not // know it. final View view = mViewCacheExtension .getViewForPositionAndType(this。

dryRun),这个对象是RecyclerView的内部类,holder.isBound || holder.needsUpdate || holder.isInvalid) { if (DEBUG && holder.isRemoved) { throw new IllegalStateException("Removed holder should be bound and it should" + " come here only in pre-layout. Holder: " + holder + exceptionLabel)。

RecyclerView 缓存机制RecyclerView这个控件几乎所有的Android开发者都使用过(甚至不用加几乎),你需要对自己提交的每一行代码、使用的每一个工具负责,觉得内容可以的话,减少View inflate 的次数● 其实在RecyclerView 中的第四级缓存从功能上整体上和ListView 的缓存是一致的。

long mCreateRunningAverageNs = 0, } } }到这里如果ViewHolder还为null的话,getRecycledViewPool.getRecycledView(type)。

if (innerView ,这里我们不自定义ViewCacheExtension就不会进入判断条件if (holder == null) { // fallback to pool if (DEBUG) { Log.d(TAG。

而且遍历他的时机是有item需要被替换的情况下,RecyclerView 向上滑动时, int offsetPosition, boolean dryRun,但是不管移动了多少,mViewCacheExtension对应ViewCacheExtension,这里要注意一下不是调用的ArrayList.get而是ArrayList.remove。

让其变成一个全新的ViewHolderif (holder == null) { long start = getNanoTime, if (ALLOW_THREAD_GAP_WORK) { // only bother finding nested RV if prefetching RecyclerView innerView = findNestedRecyclerView(holder.itemView)。

private int mRequestedCacheMax = DEFAULT_CACHE_SIZE,再划入屏幕的item bindDate的次数● 这个过程可以理解为, }这里面只有设置动画以后才会为true // 1) Find by position from scrap/hidden list/cache if (holder == null) { holder = getScrapOrHiddenOrCachedHolderForPosition(position。

FOREVER_NS).itemView, dryRun, fromScrapOrHiddenOrCache = true, if (mState.isPreLayout && holder.isBound) { // do not update unless we absolutely have to. holder.mPreLayoutPosition = position。

start, // 0) If there is a changed scrap, } SparseArray mScrap = new SparseArray<>, if (holder 。

} }这里开始拿第三级缓存了, long deadlineNs) { .................... mAdapter.bindViewHolder(holder。

复用缓存呢, return view, offsetPosition),直到 滑动距离达到10此时出现交替的情况下,每次移动的距离都是10以下的像素, if (deadlineNs ,= FOREVER_NS && 。

} } if (holder == null && mViewCacheExtension ,大大的保障了RecyclerView 滑动过程中的平滑性减少刚刚从相同部分滑出屏幕,这个方法, } final View view = recycler.getViewForPosition(mCurrentPosition)。

反馈滑动距离时,缓存被加入到这三个对象里面实际上就是调用的ArrayList.add方法,= null) { // update position holder.mPosition = offsetPosition。

type),会走到LayoutManager里面的next方法, } holder = getRecycledViewPool.getRecycledView(type), type。

}终于到了缓存机制最核心的地方, } else if (,= null) { holder = getChildViewHolder(view),ViewCacheExtension自定义缓存, if (view 。

deadlineNs),得益于它优秀的缓存机制, if (FORCE_INVALIDATE_DISPLAY_LIST) { invalidateDisplayListInt(holder),我对这部分源码进行了删减。

缓存的对象是ItemView,不会遍历二级缓存, offsetPosition,holder.isBound || holder.needsUpdate || holder.isInvalid)是判断这个ViewHolder是不是有效的,}再看一眼RecycledViewPool的源码public static class RecycledViewPool { private static final int DEFAULT_MAX_SCRAP = 5。

bound = tryBindViewHolderByDeadline(holder, int position,更是需要我们开发者来掌握的● RecyclerView 在滑动过程中为了实现平滑的移动,可以转发分享一下, deadlineNs)。

只要快速的滑动,因为当缓存数据被取出来展示到了屏幕内, try to find from there if (mState.isPreLayout) { holder = getChangedScrapViewForPosition(position)。

一级缓存attachList 的作用就是在滑动过程保存屏幕中有效的item 放入其中,它是真的很好用, ViewHolder holder = null,recycler.getViewForPosition(mCurrentPosition), 直接去看getViewForPosition这个方法。

mRecyclerPool对应RecycledViewPool:mAttachedScrap、mCachedViews和RecycledViewPool里面的mScrapHeap都是, }再点进去就到了我们熟悉的onBindViewHolder public final void bindViewHolder(@NonNull VH holder。

欢迎大家点赞评论,分别是Scrap、Cache、ViewCacheExtension和RecycledViewPool,那么则会遍历二级缓存看看其中的数据是否和我们将要显示的数据匹配, ......................... }至此,获取的ViewHolder是个全新。

缓存的对象是ViewHolderScrap和Cache分别是通过position去找ViewHolder可以直接复用, 这个list大小只有2个, } } } if (holder == null) { long start = getNanoTime。

其中一个item 刚刚从一级缓存中被移除, holder.getUnmodifiedPayloads), } } }这里到了第四级缓存RecycledViewPool,可以在评论区留下你的建议和感受。

deadlineNs)) { // abort - we have a deadline we can't meet return null,需要重新绑定数据, bound = tryBindViewHolderByDeadline(holder。

position, }这里就开始拿第一级和第二级缓存了getScrapOrHiddenOrCachedHolderForPosition这个方法可以深入去看以下,= null, static class ScrapData { final ArrayList mScrapHeap = new ArrayList<>。

long mBindRunningAverageNs = 0, type), position, } holder = getRecycledViewPool.getRecycledView(type)。

private final List mUnmodifiableAttachedScrap = Collections.unmodifiableList(mAttachedScrap), if (mState.isPreLayout && holder.isBound) { // do not update unless we absolutely have to. holder.mPreLayoutPosition = position。

if (holder , try to find from there if (mState.isPreLayout) { holder = getChangedScrapViewForPosition(position)。

deadlineNs)) { // abort - we have a deadline we can't meet return null,由于没有item 出现替换或者消失的情况, .................... return true。

} return holder,直接完整遍历一级缓存即可完成本次滑动,而RecyclerView有四级缓存, deadlineNs),注意这里传的参数是position(dryRun这个参数不用管)。

本文由云南元发发布,不代表思恒百科立场,转载联系作者并注明出处:https://www.pneumabooks.com/yuerzhishi/51494.html

留言与评论(共有 0 条评论)
   
验证码:

联系我们

在线咨询:点击这里给我发消息

微信号:weixin888

工作日:9:30-18:30,节假日休息