RecyclerView.ItemDecoration 源码分析

RecyclerView的是一个ViewGroup.会调用measureChild方法.

        public void measureChild(View child, int widthUsed, int heightUsed) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            //重点在这里.获取分割线的矩形
            final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
            //下面两行得到分割线的宽高
            widthUsed += insets.left + insets.right;
            heightUsed += insets.top + insets.bottom;
            //child的宽高加上分割线的宽高
            final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(),
                    getPaddingLeft() + getPaddingRight() + widthUsed, lp.width,
                    canScrollHorizontally());
            final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),
                    getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
                    canScrollVertically());
            if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) {
                child.measure(widthSpec, heightSpec);
            }
        }
Rect getItemDecorInsetsForChild(View child) {
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (!lp.mInsetsDirty) {
            return lp.mDecorInsets;
        }

        if (mState.isPreLayout() && (lp.isItemChanged() || lp.isViewInvalid())) {
            // changed/invalid items should not be updated until they are rebound.
            return lp.mDecorInsets;
        }
        final Rect insets = lp.mDecorInsets;
        insets.set(0, 0, 0, 0);
        final int decorCount = mItemDecorations.size();
        for (int i = 0; i < decorCount; i++) {
            mTempRect.set(0, 0, 0, 0);
            //我重写的getItemOffsets方法.见下一段代码
            mItemDecorations.get(i).getItemOffsets(mTempRect, child, this, mState);
            insets.left += mTempRect.left;
            insets.top += mTempRect.top;
            //这里就能拿到我复写方法的的outRect.right.
            insets.right += mTempRect.right;
            insets.bottom += mTempRect.bottom;
        }
        lp.mInsetsDirty = false;
        return insets;
    }
    //比如我这样复写.
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,           RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        //outRect的right和bottom都等于10
        outRect.right = 10;
        outRect.bottom = 10;
    }

再看RecyclerView的onDrawable方法

    @Override
    public void onDraw(Canvas c) {
        super.onDraw(c);

        final int count = mItemDecorations.size();
        for (int i = 0; i < count; i++) {
            //调用我继承ItemDecoration重写的onDraw方法.进行绘制
            mItemDecorations.get(i).onDraw(c, this, mState);
        }
    }
    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int childCount = parent.getChildCount();
        drawV(canvas, parent, childCount);
        drawH(canvas, parent, childCount);
    }

你可能感兴趣的:(RecyclerView.ItemDecoration 源码分析)