RecyclerView自定义分割线-android

新建一个辅助类:


import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Rect;

import android.support.annotation.ColorInt;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.view.View;

/**

* 自定义分割线

*/

public class CommItemDecorationextends RecyclerView.ItemDecoration {

private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

private int mSpace =1;//间隔

    private RectmRect =new Rect(0,0,0,0);

private PaintmPaint =new Paint();

private int mOrientation;

private CommItemDecoration(Context context,int orientation,@ColorInt int color,int space) {

mOrientation = orientation;

if(space>0){

mSpace = space;

}

mPaint.setColor(color);

}

@Override

    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

super.onDraw(c, parent, state);

if (mOrientation ==VERTICAL_LIST) {

drawVertical(c, parent);

}else {

drawHorizontal(c, parent);

}

}

public void drawVertical(Canvas c, RecyclerView parent) {

final int left = parent.getPaddingLeft();

final int right = parent.getWidth() - parent.getPaddingRight();

final int childCount = parent.getChildCount();

for (int i =0; i < childCount; i++) {

final View child = parent.getChildAt(i);

final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child

.getLayoutParams();

final int top = child.getBottom() + params.bottomMargin;

final int bottom = top +mSpace;

mRect.set(left,top,right,bottom);

c.drawRect(mRect,mPaint);

}

}

public void drawHorizontal(Canvas c, RecyclerView parent) {

final int top = parent.getPaddingTop();

final int bottom = parent.getHeight() - parent.getPaddingBottom();

final int childCount = parent.getChildCount();

for (int i =0; i < childCount; i++) {

final View child = parent.getChildAt(i);

final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child

.getLayoutParams();

final int left = child.getRight() + params.rightMargin;

final int right = left +mSpace;

mRect.set(left, top, right, bottom);

c.drawRect(mRect,mPaint);

}

}

@Override

    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

super.getItemOffsets(outRect, view, parent, state);

if (mOrientation ==VERTICAL_LIST) {

outRect.set(0,0,0,mSpace);

}else {

outRect.set(0,0,mSpace,0);

}

}

public static CommItemDecoration createVertical(Context context,@ColorInt int color,int height){

return new CommItemDecoration(context,VERTICAL_LIST,color,height);

}

public static CommItemDecoration createHorizontal(Context context,@ColorInt int color,int width){

return new CommItemDecoration(context,HORIZONTAL_LIST,color,width);

}

}



使用方式:

垂直方向:CommItemDecoration vertical = CommItemDecoration.createVertical(context, Color.BLUE,30)

//水平方向:CommItemDecoration.createHorizontal(context, Color.BLUE,30)

RecyclerView.addItemDecoration(vertical);

你可能感兴趣的:(RecyclerView自定义分割线-android)