- 使用 ItemDecoration实现间隔">注意:设置分隔间距在网格布局中会有问题。可以使用新的方法来实现,地址使用 ItemDecoration实现间隔
- 二、绘制分割线,可以设置左右间隔
- 三、地址
注意:设置分隔间距在网格布局中会有问题。可以使用新的方法来实现,地址使用 ItemDecoration实现间隔
一、不绘制,通过设置分割来实现 Item 间距,在 Item 布局中就不需要设置 margin 了
1.1 使用BaseItemIntervalDecoration来设置间距
1.BaseItemIntervalDecoration ``` public class BaseItemIntervalDecoration extends RecyclerView.ItemDecoration {
private int divider;//上下item之间 相距的距离 private int dividerHorizontal;//左右item之间 相距的距离 private int marginTop;//第一个距离上面的距离 private int marginBottom;//最后一个距离下面的距离 private int lineCount;//一行多少个 private int marginHorizontal;//距离两边的距离
public BaseItemIntervalDecoration(int divider, int lineCount) {
this.divider = divider;
this.lineCount = lineCount;
this.dividerHorizontal = divider;//默认不设置和上下的一致
}
public void setDividerHorizontal(int dividerHorizontal) {
this.dividerHorizontal = dividerHorizontal;
}
public void setMarginHorizontal(int marginHorizontal) {
this.marginHorizontal = marginHorizontal;
}
public void setMarginTop(int marginTop) {
this.marginTop = marginTop;
}
public void setMarginBottom(int marginBottom) {
this.marginBottom = marginBottom;
}
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getAdapter() != null && lineCount != 0) {
int itemCount = parent.getAdapter().getItemCount();
int position = parent.getChildLayoutPosition(view) + 1;
int lineNum = itemCount % lineCount == 0 ? itemCount / lineCount : itemCount / lineCount + 1;
int positionLineNum = position % lineCount == 0 ? position / lineCount : position / lineCount + 1;
if (positionLineNum == 1) {
outRect.top = marginTop;
}
if (lineNum == positionLineNum) {
outRect.bottom = marginBottom;
} else {
outRect.bottom = divider;
}
} else {
outRect.bottom = marginBottom;
}
if (lineCount != 0) {
if (lineCount == 1) {
outRect.left = marginHorizontal;
outRect.right = marginHorizontal;
} else if (parent.getChildLayoutPosition(view) % lineCount == 0) {
// left
outRect.left = marginHorizontal;
outRect.right = dividerHorizontal / 2;
} else if (parent.getChildLayoutPosition(view) % lineCount == (lineCount - 1)) {
outRect.left = dividerHorizontal / 2;
outRect.right = marginHorizontal;
} else {
outRect.left = dividerHorizontal / 2;
outRect.right = dividerHorizontal / 2;
}
}
} }
- 2. 使用
//使用 BaseItemIntervalDecoration 设置分割间距 //第一个参数是设置每个 item 之间的间距,后一个参数是一行展示几个 item,线性布局网格布局用法一样 val space = resources.getDimensionPixelOffset(R.dimen.dp_10) val itemDecoration = BaseItemIntervalDecoration(space,1) itemDecoration.setMarginTop(space)//设置顶部间距 itemDecoration.setMarginHorizontal(space)//设置两边间距 itemDecoration.setMarginBottom(space)//设置底部间隔
<a name="KQUaY"></a>
#### 1.2 IntervalDecotation
只适用于线性布局
- 1. IntervalDecotation
```kotlin
/**
*@FileName: IntervalDecoration
*@author : Lss Administrator
* @e-mail : kiwilss@163.com
* @time : 2020/8/1
* @desc : { 一般不管是哪个item,左右间距通常都是一样的,
* 不排除有特殊的情况,这里按照常见的情况来处理}
*/
class IntervalDecoration(
private val top: Int = 0, private val bottom: Int = 0, private val left: Int = 0, private val right: Int = 0
, private val firstTop: Int = 0, private val firstBottom: Int = bottom, private val firstLeft: Int = left, private val firstRight: Int = right,
private val lastTop: Int = top, private val lastBottom: Int = 0, private val lastLeft: Int = left, private val lastRight: Int = right)
: RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
super.getItemOffsets(outRect, view, parent, state)
//如果是第一个item
if (parent.getChildLayoutPosition(view) == 0) {
//设置四个方向的间距
outRect.top = firstTop
outRect.bottom = firstBottom
outRect.left = firstLeft
outRect.right = firstRight
}else if (parent.getChildLayoutPosition(view) + 1 == parent.adapter?.itemCount){
//最后一个item
outRect.top = lastTop
outRect.bottom = lastBottom
outRect.left = lastLeft
outRect.right =lastRight
}else{
//中间的item
outRect.top = top
outRect.bottom = bottom
outRect.left = left
outRect.right = right
}
}
}
2.使用
//设置分割线,实现item间隔,可以自由组合
it.addItemDecoration(IntervalDecoration(left = dimension,right = dimension,top = dimension))
1.3.使用 BaseItemDecoration
BaseItemDecoration
class BaseItemDecoration private constructor(private val builder: Builder) : ItemDecoration() {
private var dividerHorizontal: Int = 0//上下item之间 相距的距离vertical
private var marginTop: Int = 0 //第一个距离上面的距离 = 0
private var marginBottom = 0 //最后一个距离下面的距离 = 0
private var marginHorizontal = 0 //距离两边的距离 = 0
private var lineCount: Int = 0
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
if (parent.adapter != null && lineCount != 0) {
val itemCount = parent.adapter!!.itemCount
val position = parent.getChildLayoutPosition(view) + 1
val lineNum =
if (itemCount % lineCount == 0) itemCount / lineCount else itemCount / lineCount + 1
val positionLineNum =
if (position % lineCount == 0) position / lineCount else position / lineCount + 1
if (positionLineNum == 1) {
outRect.top = marginTop
}
if (lineNum == positionLineNum) {
outRect.bottom = marginBottom
} else {
outRect.bottom = builder.divider
}
} else {
outRect.bottom = marginBottom
}
if (lineCount != 0) {
if (lineCount == 1) {
outRect.left = marginHorizontal
outRect.right = marginHorizontal
} else if (parent.getChildLayoutPosition(view) % lineCount == 0) {
// left
outRect.left = marginHorizontal
outRect.right = dividerHorizontal / 2
} else if (parent.getChildLayoutPosition(view) % lineCount == lineCount - 1) {
outRect.left = dividerHorizontal / 2
outRect.right = marginHorizontal
} else {
outRect.left = dividerHorizontal / 2
outRect.right = dividerHorizontal / 2
}
}
}
init {
dividerHorizontal = builder.divider //默认不设置和上下的一致
lineCount = builder.lineCount//一行显示个数
}
class Builder(var divider: Int = 0, var lineCount: Int = 1) {
private val baseItemDecoration = BaseItemDecoration(this)
fun dividerHorizontal(dividerHorizontal: Int): Builder {
baseItemDecoration.dividerHorizontal = dividerHorizontal
return this
}
fun marginTop(marginTop: Int): Builder {
baseItemDecoration.marginTop = marginTop
return this
}
fun marginBottom(marginBottom: Int): Builder {
baseItemDecoration.marginBottom = marginBottom
return this
}
fun marginHorizontal(marginHorizontal: Int): Builder {
baseItemDecoration.marginHorizontal = marginHorizontal
return this
}
fun lineCount(lineCount: Int): Builder {
this.lineCount = lineCount
return this
}
fun divider(divider: Int): Builder {
this.divider = divider
return this
}
fun build() = baseItemDecoration
}
}
2.使用
//使用 BaseItemDecoration 实现同样的效果,初始间距默认 0,一列默认展示一个
val itemDecorationBase = BaseItemDecoration.Builder(space)
.marginTop(space)//顶部间距
.marginBottom(space)//底部间距
.marginHorizontal(space *2)//两侧
.dividerHorizontal(space * 2)//设置每个 item 左右之间的间隔,
.lineCount(1)//设置一列展示几行,和构造函数初始化效果一样
.divider(space * 2)//设置每个 item 上下之间的间隔,和构造函数初始化效果一样
.build()
二、绘制分割线,可以设置左右间隔
库中类名修改成下面三个:
new LinearDividerDecoration.Builder();
new GridDividerDecoration.Builder();
new ItemDivider();
2.1 线性布局,可以设置最后一行是否绘制
```kotlin public class LinearItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider; private boolean mShowLastLine; private int mSpanSpace = 2; private int mLeftPadding; private int mRightPadding;
public LinearItemDecoration(int span,int leftPadding,int rightPadding,int color,boolean show){
mSpanSpace = span;
mShowLastLine = show;
mLeftPadding = leftPadding;
mRightPadding = rightPadding;
mDivider = new ColorDrawable(color);
}
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int count = mShowLastLine ? parent.getAdapter().getItemCount() : parent.getAdapter().getItemCount() - 1;
if (isVertical(parent)) {
if (parent.getChildAdapterPosition(view) < count) {
outRect.set(0, 0, 0, mSpanSpace);
} else {
outRect.set(0, 0, 0, 0);
}
} else {
if (parent.getChildAdapterPosition(view) < count) {
outRect.set(0, 0, mSpanSpace, 0);
} else {
outRect.set(0, 0, 0, 0);
}
}
}
private boolean isVertical(RecyclerView parent) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
int orientation = ((LinearLayoutManager) layoutManager) .getOrientation();
return orientation == LinearLayoutManager.VERTICAL;
}
return false;
}
@Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (isVertical(parent)) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft() + mLeftPadding;
final int right = parent.getWidth() - parent.getPaddingRight() - mRightPadding;
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 + Math.round(ViewCompat.getTranslationY(child));
final int bottom = top + mSpanSpace; int count = mShowLastLine ? parent.getAdapter().getItemCount() : parent.getAdapter().getItemCount() - 1;
if (i < count) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
} else {
mDivider.setBounds(left, top, right, top);
mDivider.draw(c);
}
}
}
private 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 + Math.round(ViewCompat.getTranslationX(child));
final int right = left + mSpanSpace; int count = mShowLastLine ? parent.getAdapter().getItemCount() : parent.getAdapter().getItemCount() - 1;
if (i < count) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
/**
- Builder模式
*/ public static class Builder{
private Context mContext; private Resources mResources; private int mSpanSpace; private boolean mShowLastLine; private int mLeftPadding; private int mRightPadding; private int mColor;
public Builder(Context context){
mContext = context;
mResources = context.getResources();
mSpanSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 1f, context.getResources().getDisplayMetrics());
mLeftPadding = 0;
mRightPadding = 0;
mShowLastLine = false;
mColor = Color.BLACK;
}
/**
设置分割线宽(高)度 */ public Builder setSpan(float pixels) { mSpanSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, pixels, mResources.getDisplayMetrics()); return this;
}/**
设置分割线宽(高)度 */ public Builder setSpan(@DimenRes int resource) { mSpanSpace = mResources.getDimensionPixelSize(resource); return this; }
/**
设置左右间距 */ public Builder setPadding(float pixels) { setLeftPadding(pixels); setRightPadding(pixels); return this; }
/**
设置左右间距 */ public Builder setPadding(@DimenRes int resource) { setLeftPadding(resource); setRightPadding(resource); return this; }
/**
设置左间距 */ public Builder setLeftPadding(float pixelPadding) { mLeftPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, pixelPadding, mResources.getDisplayMetrics()); return this; }
/**
设置右间距 */ public Builder setRightPadding(float pixelPadding) { mRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, pixelPadding, mResources.getDisplayMetrics()); return this; }
/**
通过资源id设置左间距 */ public Builder setLeftPadding(@DimenRes int resource) { mLeftPadding = mResources.getDimensionPixelSize(resource); return this; }
/**
通过资源id设置右间距 */ public Builder setRightPadding(@DimenRes int resource) { mRightPadding = mResources.getDimensionPixelSize(resource); return this; }
/**
通过资源id设置颜色 */ public Builder setColorResource(@ColorRes int resource) { setColor(ContextCompat.getColor(mContext,resource)); return this; }
/**
设置颜色 */ public Builder setColor(@ColorInt int color) { mColor = color; return this; }
/**
- 是否最后一条显示分割线
*/ public Builder setShowLastLine(boolean show){ mShowLastLine = show; return this; }
/**
- Instantiates a LinearItemDecoration with the specified parameters.
- @return a properly initialized LinearItemDecoration instance */ public LinearItemDecoration build() { return new LinearItemDecoration(mSpanSpace,mLeftPadding,mRightPadding,mColor,mShowLastLine); }
}
}
使用:
```kotlin
val divider = LinearItemDecoration.Builder(this)
.setSpan(20f)
.setPadding(R.dimen.m10)
// .setLeftPadding(R.dimen.m10)
// .setRightPadding(R.dimen.m10)
.setColorResource(R.color.red)
.setShowLastLine(true)
.build()
//设置自定义分割线
it.addItemDecoration(divider)
2.2 网格布局,可以设置最后一行底部是否绘制,最后一个 item 右侧不会绘制
效果图:
public class GridItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private boolean mShowLastLine;
private int mHorizonSpan;
private int mVerticalSpan;
private GridItemDecoration(int horizonSpan,int verticalSpan,int color,boolean showLastLine) {
this.mHorizonSpan = horizonSpan;
this.mShowLastLine = showLastLine;
this.mVerticalSpan = verticalSpan;
mDivider = new ColorDrawable(color);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
//最后一行底部横线不绘制
if (isLastRaw(parent,i,getSpanCount(parent),childCount) && !mShowLastLine){
continue;
}
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin;
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mHorizonSpan;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
if((parent.getChildViewHolder(child).getAdapterPosition() + 1) % getSpanCount(parent) == 0){
continue;
}
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin + mHorizonSpan;
final int left = child.getRight() + params.rightMargin;
int right = left + mVerticalSpan;
// //满足条件( 最后一行 && 不绘制 ) 将vertical多出的一部分去掉;
if (i==childCount-1) {
right -= mVerticalSpan;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 计算偏移量
* */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
if (itemPosition < 0){
return;
}
int column = itemPosition % spanCount;
int bottom;
int left = column * mVerticalSpan / spanCount;
int right = mVerticalSpan - (column + 1) * mVerticalSpan / spanCount;
if (isLastRaw(parent, itemPosition, spanCount, childCount)){
if (mShowLastLine){
bottom = mHorizonSpan;
}else{
bottom = 0;
}
}else{
bottom = mHorizonSpan;
}
outRect.set(left, 0, right, bottom);
}
/**
* 获取列数
* */
private int getSpanCount(RecyclerView parent) {
// 列数
int mSpanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
mSpanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
mSpanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
return mSpanCount;
}
/**
* 是否最后一行
* @param parent RecyclerView
* @param pos 当前item的位置
* @param spanCount 每行显示的item个数
* @param childCount child个数
* */
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
return getResult(pos,spanCount,childCount);
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
// StaggeredGridLayoutManager 且纵向滚动
return getResult(pos,spanCount,childCount);
} else {
// StaggeredGridLayoutManager 且横向滚动
if ((pos + 1) % spanCount == 0) {
return true;
}
}
}
return false;
}
private boolean getResult(int pos,int spanCount,int childCount){
int remainCount = childCount % spanCount;//获取余数
//如果正好最后一行完整;
if (remainCount == 0){
if(pos >= childCount - spanCount){
return true; //最后一行全部不绘制;
}
}else{
if (pos >= childCount - childCount % spanCount){
return true;
}
}
return false;
}
/**
* 使用Builder构造
* */
public static class Builder {
private Context mContext;
private Resources mResources;
private boolean mShowLastLine;
private int mHorizonSpan;
private int mVerticalSpan;
private int mColor;
public Builder(Context context) {
mContext = context;
mResources = context.getResources();
mShowLastLine = true;
mHorizonSpan = 0;
mVerticalSpan = 0;
mColor = Color.WHITE;
}
/**
* 通过资源文件设置分隔线颜色
*/
public Builder setColorResource(@ColorRes int resource) {
setColor(ContextCompat.getColor(mContext, resource));
return this;
}
/**
* 设置颜色
*/
public Builder setColor(@ColorInt int color) {
mColor = color;
return this;
}
/**
* 通过dp设置垂直间距
* */
public Builder setVerticalSpan(@DimenRes int vertical) {
this.mVerticalSpan = mResources.getDimensionPixelSize(vertical);
return this;
}
/**
* 通过px设置垂直间距
* */
public Builder setVerticalSpan(float mVertical) {
this.mVerticalSpan = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mVertical, mResources.getDisplayMetrics());
return this;
}
/**
* 通过dp设置水平间距
* */
public Builder setHorizontalSpan(@DimenRes int horizontal) {
this.mHorizonSpan = mResources.getDimensionPixelSize(horizontal);
return this;
}
/**
* 通过px设置水平间距
* */
public Builder setHorizontalSpan(float horizontal) {
this.mHorizonSpan = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, horizontal, mResources.getDisplayMetrics());
return this;
}
/**
* 是否最后一条显示分割线
* */
public GridItemDecoration.Builder setShowLastLine(boolean show){
mShowLastLine = show;
return this;
}
public GridItemDecoration build() {
return new GridItemDecoration(mHorizonSpan, mVerticalSpan, mColor,mShowLastLine);
}
}
}
使用:
val color = ContextCompat.getColor(this, R.color.red)
val divider = GridItemDecoration.Builder(this)
.setColor(color)
.setHorizontalSpan(R.dimen.m10)
.setVerticalSpan(R.dimen.m10)
.setShowLastLine(false)
.build()
//设置自定义分割线
it.addItemDecoration(divider)
2.3 网格布局分割线,底部自己判断是否绘制,右侧最后一个 item 会绘制,缺点线条粗细不一
效果图:
public class ItemDivider extends RecyclerView.ItemDecoration {
private int dividerWith = 4;
private Paint paint;
private RecyclerView.LayoutManager layoutManager;
// 构造方法,可以在这里做一些初始化,比如指定画笔颜色什么的
public ItemDivider() {
initPaint();
paint.setColor(0xffE6E6E6);
}
private void initPaint() {
if (paint == null) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
}
}
public ItemDivider setDividerWith(int dividerWith) {
this.dividerWith = dividerWith;
return this;
}
public ItemDivider setDividerColor(int color) {
initPaint();
paint.setColor(color);
return this;
}
/**
* 指定item之间的间距(就是指定分割线的宽度) 回调顺序 1
* @param outRect Rect to receive the output.
* @param view The child view to decorate
* @param parent RecyclerView this ItemDecoration is decorating
* @param state The current state of RecyclerView.
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (layoutManager == null) {
layoutManager = parent.getLayoutManager();
}
// 适用 LinearLayoutManager 和 GridLayoutManager
if (layoutManager instanceof LinearLayoutManager) {
int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
if (orientation == LinearLayoutManager.VERTICAL) {
// 水平分割线将绘制在item底部
outRect.bottom = dividerWith;
} else if (orientation == LinearLayoutManager.HORIZONTAL) {
// 垂直分割线将绘制在item右侧
outRect.right = dividerWith;
}
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
// 如果是 GridLayoutManager 则需要绘制另一个方向上的分割线
if (orientation == LinearLayoutManager.VERTICAL && lp != null && lp.getSpanIndex() > 0) {
// 如果列表是垂直方向,则最左边的一列略过
outRect.left = dividerWith;
} else if (orientation == LinearLayoutManager.HORIZONTAL && lp != null && lp.getSpanIndex() > 0) {
// 如果列表是水平方向,则最上边的一列略过
outRect.top = dividerWith;
}
}
}
}
/**
* 在item 绘制之前调用(就是绘制在 item 的底层) 回调顺序 2
* 一般分割线在这里绘制
* 看到canvas,对自定义控件有一定了解的话,就能想到为什么说给RecyclerView设置分割线更灵活了
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView
*/
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
int spanCount = 0;
if (layoutManager instanceof GridLayoutManager){
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
}
int childCount = parent.getChildCount();
int count = childCount % spanCount;
if (count == 0){
count = spanCount;
}
// 这个值是为了补偿横竖方向上分割线交叉处间隙
int offSet = (int) Math.ceil(dividerWith * 1f / 2);
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left1 = child.getRight() + params.rightMargin;
int right1 = left1 + dividerWith;
int top1 = child.getTop() - offSet - params.topMargin;
int bottom1 = child.getBottom() + offSet + params.bottomMargin;
//绘制分割线(矩形)
c.drawRect(left1, top1, right1, bottom1, paint);
int left2 = child.getLeft() - offSet - params.leftMargin;
int right2 = child.getRight() + offSet + params.rightMargin;
int top2 = child.getBottom() + params.bottomMargin;
int bottom2 = top2 + dividerWith;
//绘制分割线(矩形)
//c.drawRect(left2, top2, right2, bottom2, paint);
//判断最后一行不画
if (i < parent.getChildCount() -count){
c.drawRect(left2, top2, right2, bottom2, paint);
}
}
}
/**
* 在item 绘制之后调用(就是绘制在 item 的上层) 回调顺序 3
* 也可以在这里绘制分割线,和上面的方法 二选一
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
}
使用:
//设置自定义分割线
it.addItemDecoration(ItemDivider().setDividerColor(color))