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); finalint decorCount = mItemDecorations.size(); for (int i = 0; i < decorCount; i++) { mTempRect.set(0, 0, 0, 0); //获得保存在Rect中的上下左右的offset mItemDecorations.get(i).getItemOffsets(mTempRect, child, this, mState); insets.left += mTempRect.left; insets.top += mTempRect.top; insets.right += mTempRect.right; insets.bottom += mTempRect.bottom; } lp.mInsetsDirty = false; return insets; }
//假如竖直方向布局 voidlayoutChunk(){ 。。。 measureChildWithMargins(view, 0, 0); //getDecoratedMeasurement返回就是竖直整个子View包括ItemDecoration的所占用的高度 result.mConsumed = mOrientationHelper.getDecoratedMeasurement(view); int left, top, right, bottom; if (mOrientation == VERTICAL) { //左边界,只是RV的padding left = getPaddingLeft(); //右边界包括子view的宽度、左右margin以及RV.LayoutParams的mDecorInsets right = left + mOrientationHelper.getDecoratedMeasurementInOther(view); //上边界就是整体的偏移 top = layoutState.mOffset; //下边界包括子View的高度、上下margin以及RV.LayoutParams的mDecorInsets bottom = layoutState.mOffset + result.mConsumed;
} else { 。。。 } // We calculate everything with View's bounding box (which includes decor and margins) // To calculate correct layout position, we subtract margins. layoutDecoratedWithMargins(view, left, top, right, bottom); }
publicvoidlayoutDecoratedWithMargins(@NonNull View child, int left, int top, int right, int bottom) final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final Rect insets = lp.mDecorInsets; //真正布局确定子View的上下左右边界时去除了margin和mDecorInsets child.layout(left + insets.left + lp.leftMargin, top + insets.top + lp.topMargin, right - insets.right - lp.rightMargin, bottom - insets.bottom - lp.bottomMargin); }
/** * Returns the index of the child to draw for this iteration. Override this * if you want to change the drawing order of children. By default, it * returns i. * <p> * NOTE: In order for this method to be called, you must enable child ordering * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. * * @param i The current iteration. * @return The index of the child to draw this iteration. * * @see #setChildrenDrawingOrderEnabled(boolean) * @see #isChildrenDrawingOrderEnabled() */ protectedintgetChildDrawingOrder(int childCount, int i){ return i; }