RecyclerView系列--写一个属于你自己的ItemDecoration

前言

相信RecyclerView大家一定很熟悉吧?它是谷歌在高级版本提出一个新的替代ListViewGridView的控件,高度解耦,自带了ViewHolder性能优化。你可以使用它进行分割线ItemDecoration、布局LayoutManager以及刷新策略notifyxxDiffutil等多种定制操作,让你爽翻天。
下面介绍一下ItemDecoration定制操作,编写一个尽可能通用的分割线。

第一步

确定你所有的需求,item的方向、分割线的粗细、颜色、距离左边和右边的边距(ps:以前使用listview为了实现美工的左右边距,经常隐藏自带的分割线,额外再item布局的最下面添加一个view分割线)。有了ItemDecoration就不需要这样做。

第二步

继承RecyclerView.ItemDecoration``重写getItemOffsetsonDraw“`方法。

  • public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
    outRect确定分割线的大小范围
  • public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state)
    绘制分割线
public class CustomItemDecoration extends RecyclerView.ItemDecoration {
    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
    }
}

第三步

定义你想要的属性,参照第一步选择了如下属性。

private int dividerWidth;
private int leftPadding;
private int topPadding;
private int bottomPadding;
private int rightPadding;
private int dividerColor;

是不是缺少一个方向呢?其实可以通过RecyclerView parent这个参数来获取到。

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    //super.onDraw(c, parent, state);
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        int orientation = linearLayoutManager.getOrientation();
        if (orientation == LinearLayoutManager.VERTICAL) {
            drawVerticalDivider(canvas, parent);
        } else if (orientation == LinearLayoutManager.HORIZONTAL) {

            drawHorizontalDivider(canvas, parent);
        }
    } else {
        throw new IllegalArgumentException("please use LinearLayoutManager");
    }
}

第四步

根据方向确定想要的风格线的大小范围

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    //super.getItemOffsets(outRect, view, parent, state);
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        int orientation = linearLayoutManager.getOrientation();
        if (orientation == LinearLayoutManager.VERTICAL) {
            outRect.set(leftPadding, 0, rightPadding, dividerWidth);
        } else if (orientation == LinearLayoutManager.HORIZONTAL) {
            outRect.set(0, topPadding, dividerWidth, bottomPadding);
        }
    } else {
        throw new IllegalArgumentException("please use LinearLayoutManager");
    }
}

第五步

绘制分割线

private void drawVerticalDivider(Canvas canvas, RecyclerView parent) {
     int childCount = parent.getChildCount();
     for (int index = 0; index < childCount; index++) {
         View child = parent.getChildAt(index);
         RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
         final int top = child.getBottom() + layoutParams.bottomMargin;
         final int bottom = top + dividerWidth;
         canvas.drawRect(child.getLeft() + leftPadding, top, child.getRight() - rightPadding, bottom, mPaint);
     }
 }

 private void drawHorizontalDivider(Canvas canvas, RecyclerView parent) {
     int childCount = parent.getChildCount();
     for (int index = 0; index < childCount; index++) {
         View child = parent.getChildAt(index);
         RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
         final int left = child.getRight() + layoutParams.rightMargin;
         final int right = left + dividerWidth;
         canvas.drawRect(left, child.getTop() + topPadding, right, child.getBottom() - bottomPadding, mPaint);
     }
 }

大公告成,使用一下看看

CustomItemDecoration itemDecoration = new CustomItemDecoration((int) ScreenUtils.dip2px(1));
itemDecoration.setLeftPadding((int) ScreenUtils.dip2px(15));
itemDecoration.setRightPadding((int) ScreenUtils.dip2px(15));
recyclerView.addItemDecoration(itemDecoration);

RecyclerView系列--写一个属于你自己的ItemDecoration_第1张图片

总结:其实这属于RecyclerView定制操作的最基础的操作,只需要重写两个方法,一个确定范围,一个绘制。

你可能感兴趣的:(android)