RecyclerView回收复用实现学习笔记

今天继续启舰的自定义控件三部曲视图篇(八)——RecyclerView系列之五回收复用实现方式二的学习,记录下自己的理解。
自定义控件三部曲视图篇(八)——RecyclerView系列之五回收复用实现方式二

直接Copy实现原理:
在这里,我们主要替换掉在上节中移动item所用的offsetChildrenVertical(-travel);函数,既然要将它弃用,那我们就只能自己布局每个item了。很明显,在这里我们主要处理的是滚动的情况,对于onLayoutChildren中的代码是不用改动的。

试想,在滚动dy时,有两种item需要重新布局:

第一种:原来已经在屏幕上的item
第二种:新增的item
所以,这里就涉及到怎么处理已经在屏幕上的item和新增item的重绘问题,我们可以效仿在onLayoutChildren中的处理方式,先调用detachAndScrapAttachedViews(recycler)将屏幕上已经在显示的所有Item离屏,然后再将所有item重绘。

那第二个问题又来了,我们应该从哪个item开始重绘,到哪个item结束呢?
很明显,在向下滚动时,低部Item下移,顶部空出来空白区域。所以我们只需要从当前在显示的Item向前遍历,直到index=0即可。

当向上滚动时,顶部Item上移,底部空出来空白区域。所以我们也只需要从当前在显示的顶部Item向上遍历,直到Item结束为止。

优化方法:
上面方法是通过使用detachAndScrapAttachedViews(recycler) 将所有的item离屏缓存,然后通过布局所有与的item实现回收复用,

改进方法:将所有已经在屏幕上的item直接布局; 我们在回收越界item时,会遍历所有的可见item,所以我们可以把它放在回收越界时,如果越界就回收,如果没越界就重新布局:

具体实现方法是:
使用一个变量来保存item是否已经布局:
private SparseBooleanArray mHasAttachedItems = new SparseBooleanArray();

如果需要回收的item,就先移除,并设置mHasAttachedItems设置为false,如果不需要回收的直接重新布局就可以,具体代码如下:

        for (int i = getChildCount() - 1; i>=0; i--) {
            View child = getChildAt(i);
            int pos = getPosition(child);
            Rect rect = mItemRects.get(pos);
            if (!Rect.intersects(rect, visibleRect)){
            //不在可见区域就回收,同时mHasAttachedItems中设置为false
                removeAndRecycleView(child, recycler);
                mHasAttachedItems.put(pos, false);
            }else {
            //不需要回收的item直接冲新布局,并设置mHasAttachedItems中为true,同时设置旋转
                layoutDecoratedWithMargins(child, rect.left, rect.top - mSumDy, rect.right, rect.bottom - mSumDy);
                child.setRotationY(child.getRotationY() + 1);

                mHasAttachedItems.put(pos, true);
            }
        }

在复用的时候,先将屏幕上第一个item与最后一个item取出,在向上滑动时,从firstView 往上遍历,往下滑动时,从lastView往后遍历,将在visibleRect区域内的item 加入,并设置,将mHasAttachedItems.put(pos, true);具体代码如下


        View lastView = getChildAt(getChildCount() - 1);
        View firstView = getChildAt(0);

        mSumDy += travel;
         //计算RecyclerView中在屏幕可见的item区域
        if (travel >= 0) { //向上滑动, 当前滚动的Item向后遍历

            int minPos = getPosition(firstView); // 找到最下面item在RecyclerView中的位置
            //可以使用此优化条件
            //int MAX = (minPos + 20 > getItemCount()) ? getItemCount() - 1 : minPos + 20;

            for (int i = minPos; i <= getItemCount() - 1; i++) { //遍历下面不可见的item
                insertView(i, visibleRect, recycler, false); //可以优化,不需要遍历到最后面
            }
        }else { //向下滑动

            int maxPos = getPosition(lastView); //即将出现的item的最大position
            for (int i = maxPos; i >= 0; i--) {
                insertView(i, visibleRect, recycler, true);
            }
        }

private void insertView(int pos, Rect visibleRect, RecyclerView.Recycler recycler, boolean firstPos) {
        Rect rect = mItemRects.get(pos);
        if (Rect.intersects(visibleRect, rect) && !mHasAttachedItems.get(pos)){ //判断是否在可见区域
            View child = recycler.getViewForPosition(pos);
            if (firstPos) {
                addView(child, 0);
            } else {
                addView(child);
            }
            measureChildWithMargins(child, 0, 0);
            layoutDecoratedWithMargins(child, rect.left, rect.top - mSumDy, rect.right, rect.bottom - mSumDy);
            child.setRotationY(child.getRotationY()+1);
            mHasAttachedItems.put(pos, true);
        }
    }

完整代码如下:

public class CustomLayoutManager2 extends RecyclerView.LayoutManager {
    private int mTotalHeight;
    private int mItemWidth, mItemHeight;
    private SparseArray<Rect> mItemRects = new SparseArray<>();
    //存放是否已经布局,当回收的时候设置成false
    private SparseBooleanArray mHasAttachedItems = new SparseBooleanArray();

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
    }

    private int getVerticalSpace() {
        return getHeight() - getPaddingTop() - getPaddingBottom();
    }


    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

        mItemRects.clear();
        mHasAttachedItems.clear();

        if (getItemCount() == 0) { //没有item
            detachAndScrapAttachedViews(recycler);
            return;
        }
        detachAndScrapAttachedViews(recycler);

        View childView = recycler.getViewForPosition(0);
        measureChildWithMargins(childView, 0, 0);
        mItemWidth = getDecoratedMeasuredWidth(childView);
        mItemHeight = getDecoratedMeasuredHeight(childView);
        int visibleCount = getVerticalSpace() / mItemHeight; //为考虑除不完,还有就是没有考虑decoration

        int offsetY = 0;
        for (int i = 0; i < getItemCount(); i++) {
            Rect rect = new Rect(0, offsetY, mItemWidth, offsetY + mItemHeight);
            mItemRects.put(i, rect);
            mHasAttachedItems.put(i, false);
            offsetY += mItemHeight;
        }

        for (int i = 0; i < visibleCount; i++) {
            Rect rect = mItemRects.get(i);
            View view = recycler.getViewForPosition(i);
            addView(view);
            measureChildWithMargins(view, 0, 0);
            layoutDecorated(view, rect.left, rect.top, rect.right, rect.bottom);
        }
        mTotalHeight = Math.max(offsetY, getVerticalSpace());
    }

    @Override
    public boolean canScrollVertically() {
        return true;
    }

    private int mSumDy = 0;
    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int travel = dy;
        if (mSumDy + dy < 0) {
            travel = -mSumDy;
        } else if (mSumDy + dy > mTotalHeight - getVerticalSpace()) {
            travel = mTotalHeight - getVerticalSpace() - mSumDy;
        }

        /**
         * 搞定回收
         */
        Rect visibleRect = getVisibleArea();
        for (int i = getChildCount() - 1; i>=0; i--) {
            View child = getChildAt(i);
            int pos = getPosition(child);
            Rect rect = mItemRects.get(pos);
            if (!Rect.intersects(rect, visibleRect)){
                removeAndRecycleView(child, recycler);
                mHasAttachedItems.put(pos, false);
            }else {
                layoutDecoratedWithMargins(child, rect.left, rect.top - mSumDy, rect.right, rect.bottom - mSumDy);
                child.setRotationY(child.getRotationY() + 1);

                mHasAttachedItems.put(pos, true);
            }

        }
        /**
         * 搞定复用
         */
        View lastView = getChildAt(getChildCount() - 1);
        View firstView = getChildAt(0);

        mSumDy += travel;
         //计算RecyclerView中在屏幕可见的item区域
        if (travel >= 0) { //向上滑动, 当前滚动的Item向后遍历

            int minPos = getPosition(firstView); // 找到最下面item在RecyclerView中的位置
            //可以使用此优化条件
            int MAX = (minPos + 20 > getItemCount()) ? getItemCount() - 1 : minPos + 20;

            for (int i = minPos; i <= getItemCount() - 1; i++) { //遍历下面不可见的item
                insertView(i, visibleRect, recycler, false); //可以优化,不需要遍历到最后面
            }
        }else { //向下滑动

            int maxPos = getPosition(lastView); //即将出现的item的最大position
            for (int i = maxPos; i >= 0; i--) {
                insertView(i, visibleRect, recycler, true);
            }
        }

        return travel;
    }

    private void insertView(int pos, Rect visibleRect, RecyclerView.Recycler recycler, boolean firstPos) {
        Rect rect = mItemRects.get(pos);
        if (Rect.intersects(visibleRect, rect) && !mHasAttachedItems.get(pos)){ //判断是否在可见区域
            View child = recycler.getViewForPosition(pos);
            if (firstPos) {
                addView(child, 0);
            } else {
                addView(child);
            }
            measureChildWithMargins(child, 0, 0);
            layoutDecoratedWithMargins(child, rect.left, rect.top - mSumDy, rect.right, rect.bottom - mSumDy);
            child.setRotationY(child.getRotationY()+1);
            mHasAttachedItems.put(pos, true);
        }
    }

    private Rect getVisibleArea() {
        Rect result = new Rect(getPaddingLeft(), getPaddingTop() + mSumDy, getWidth() + getPaddingRight(), getVerticalSpace() + mSumDy);
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值