RecyclerView添加自定义ItemDecoration实现(2)

1.看过第一篇文章的都会注意到在设置RecyclerView的GridLayoutManager的Item时设置的是正方形的布局。可是当设置自定义分割线的时候出现了一些问题。如果对GridLayoutManager添加分割线有疑惑的可以查看上一篇文章。

RecyclerView添加自定义ItemDecoration实现(1)

具体情况如下图所示:

《RecyclerView添加自定义ItemDecoration实现(2)》 GridLayotManager_bug_5dp.png

问题:从图中可能不能很好的看出存在的问题。我们不妨加大分割线的宽度之后再观察。所以我们将之前分割线的宽度改为20dp。效果如下:

《RecyclerView添加自定义ItemDecoration实现(2)》 drawable_item_decoration_20dp.png
《RecyclerView添加自定义ItemDecoration实现(2)》 GridLayoutManager_bug_20dp.png

现在可以很明显的看出之前遗留下来的bug:第一列和第二列的形状和第三列的不同。那么为什么会出现这种情况呢?我们下面从源码分析一下添加ItemDecoration的原理。

2.在RecyclerView中有如下一部分代码

/**
 * Measure a child view using standard measurement policy, taking the padding
 * of the parent RecyclerView and any added item decorations into account.
 *
 * <p>If the RecyclerView can be scrolled in either dimension the caller may
 * pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
 *
 * @param child Child view to measure
 * @param widthUsed Width in pixels currently consumed by other views, if relevant
 * @param heightUsed Height in pixels currently consumed by other views, if relevant
 */
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;
    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);
    }
}



/**
 * Calculate a MeasureSpec value for measuring a child view in one dimension.
 *
 * @param parentSize Size of the parent view where the child will be placed
 * @param parentMode The measurement spec mode of the parent
 * @param padding Total space currently consumed by other elements of parent
 * @param childDimension Desired size of the child view, or MATCH_PARENT/WRAP_CONTENT.
 *                       Generally obtained from the child view's LayoutParams
 * @param canScroll true if the parent RecyclerView can scroll in this dimension
 *
 * @return a MeasureSpec value for the child view
 */
public static int getChildMeasureSpec(int parentSize, int parentMode, int padding,
        int childDimension, boolean canScroll) {
    int size = Math.max(0, parentSize - padding);
    int resultSize = 0;
    int resultMode = 0;
    if (childDimension >= 0) {
        resultSize = childDimension;
        resultMode = MeasureSpec.EXACTLY;
    } else if (canScroll) {
         if (childDimension == LayoutParams.MATCH_PARENT){
            switch (parentMode) {
                case MeasureSpec.AT_MOST:
                case MeasureSpec.EXACTLY:
                    resultSize = size;
                    resultMode = parentMode;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    resultSize = 0;
                    resultMode = MeasureSpec.UNSPECIFIED;
                    break;
            }
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {
            resultSize = 0;
            resultMode = MeasureSpec.UNSPECIFIED;
        }
    } else {
        if (childDimension == LayoutParams.MATCH_PARENT) {
            resultSize = size;
            resultMode = parentMode;
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {
            resultSize = size;
            if (parentMode == MeasureSpec.AT_MOST || parentMode == MeasureSpec.EXACTLY) {
                resultMode = MeasureSpec.AT_MOST;
            } else {
                resultMode = MeasureSpec.UNSPECIFIED;
            }

        }
    }
    //noinspection WrongConstant
    return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}

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);
        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;
}
  • 首先我们先看 getChildMeasureSpec 方法。 当 childDimension >= 0 时让结果为 childDimension,否则为 Math.max(0, parentSize – padding)。这个的意思就是如果子view的尺寸如果是精确值的话,那么就是精确值的结果,如果是 MATCH_PARENT 或者 WRAP_CONTENT 的话就开始计算结果。
  • 其次我们从 measureChild 方法中可以看出,widthSpec 的 getChildMeasureSpec 中可以得到

     paddingg =  getPaddingLeft() + getPaddingRight() + widthUsed;
    

     final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
     widthUsed += insets.left + insets.right;
    
  • 最后我们可以从 getItemDecorInsetsForChild 方法中可以看出 Rect insets就是在自定义 ItemDecoration 时
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {} 中的outRect。所以计算 widthUsed 时候,与 outRect 有关,而我们在 getItemOffsets 方法中有这么一段代码

     if (!isLastColumn(currentItemPosition, mSpanCount)) {
          outRect.right = mDrawable.getIntrinsicWidth();
     } else {
         outRect.right = 0;
     }
    

    如果是最后一列的话,outRect.right = 0;这使得第三列子view的 widthUsed 与前两列的不同,所以第三列子view的 padding 就会比前两列的少一个分割线的宽度。

3.解决

那我们应该怎么解决这个问题呢?问题的关键就是分割线的宽度分配不均导致的,我们只要均匀分配宽度即可。

《RecyclerView添加自定义ItemDecoration实现(2)》 statement.png

如图所示,我们对于有三列的情况来说,averWidth = (2 * dividerWidth)/3, 将两个分割线的宽度,均分给3个子 view 的 outRect。所以为了适配其他情况得到如下式子 :

 averWidth = [(spanCount - 1 ) * dividerWidth ]/spanCount ;

现在是最重要的步骤,如果将averWidth分给每个子 view,就会存在如下情况:

leftN + rightN = averWidth;
left(N+1) + rigthN = dividerWidth;
(注:leftN为第N个view的左偏移值,rightN为第N个View的右偏移值,计数从0开始)

对于 rightN 存在如下的情况:

 right0 = averWidth - left0;           
 right1 = averWidth - left1;           
 right2 = averWidth - left2;           
 .
 .
 .
 rightN = averWidth - leftN;

对于 leftN 存在如下的情况:

left0 = 0;                
left1 =  dividerWidth - right0;
left2 =  dividerWidth - right1;
.
.  
.
leftN = dividerWidth - right(N-1);

从上面的式子可以看出,我们只要得到 leftN 的就能很快的得到 rightN,所以将 rightN 式子合并到 leftN 中得到如下的结果:

  left0 = 0;

  left1 = dividerWidth - (averWidth - left0) 
        = dividerWidth - averWidth + left0
        = dividerWidth - averWidth 
  
  left2 = dividerWidth - (averWidth  - left1) 
        = dividerWidth -[averWidth  - (dividerWidth - averWidth)] 
        = 2 * (dividerWidth - averWidth)    
  .
  .
  .
  leftN = N * (dividerWidth - averWidth)

所以,根据化简后的结果更改代码,修改完后的 getItemOffsets 方法具体如下:

/**
 * @param outRect 用于规定分割线的范围
 * @param view    进行分割线操作的子view
 * @param parent  父view
 * @param state   (这里暂时不使用)
 */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    //notification的时候要获取
    mTotalItems = parent.getAdapter().getItemCount();
    if (0 == mSpanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        //判断是否为GridLayoutManager
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            mSpanCount = gridLayoutManager.getSpanCount();
        } else {
            mSpanCount = 1;
        }
    }
    //在源码中有一个过时的方法,里面有获取当前ItemPosition
    int currentItemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
    //
    if (!isLastRow(currentItemPosition, mTotalItems, mSpanCount))
        outRect.bottom = mDrawable.getIntrinsicWidth();
    else
        outRect.bottom = 0;
    //将isLastColumn注释,添加下面代码
    //        if (!isLastColumn(currentItemPosition, mSpanCount)) {
    //            outRect.right = mDrawable.getIntrinsicWidth();
    //        } else {
    //            outRect.right = 0;
    //        }
    int averWidth = (mSpanCount - 1) * mDrawable.getIntrinsicWidth() / mSpanCount;
    int dX = mDrawable.getIntrinsicWidth() - averWidth;
    outRect.left = (currentItemPosition % mSpanCount) * dX;
    outRect.right = averWidth - outRect.left;
}

结果截图:

《RecyclerView添加自定义ItemDecoration实现(2)》 final.png

    原文作者:请叫我张懂
    原文地址: https://www.jianshu.com/p/a8b00a02a46a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞