Skip to content

Commit

Permalink
add 线性布局分割线增加直接设置颜色高度等
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jan 3, 2020
1 parent db8db3f commit 4654bdd
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

Expand Down Expand Up @@ -59,7 +59,26 @@ public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
* Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}.
*/
private int mOrientation;
private Paint mPaint;
/**
* 如果是横向 - 宽度
* 如果是纵向 - 高度
*/
private int mDividerSpacing;
/**
* 如果是横向 - 左边距
* 如果是纵向 - 上边距
*/
private int mLeftTopPadding;
/**
* 如果是横向 - 右边距
* 如果是纵向 - 下边距
*/
private int mRightBottomPadding;

public SpacesItemDecoration(Context context) {
this(context, VERTICAL, 0, 1);
}

public SpacesItemDecoration(Context context, int orientation) {
this(context, orientation, 0, 1);
Expand All @@ -85,9 +104,6 @@ public SpacesItemDecoration(Context context, int orientation, int headerNoShowSi
setOrientation(orientation);
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
if (mDivider == null) {
Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this " + "SpacesItemDecoration. Please set that attribute all call setDrawable()");
}
a.recycle();
}

Expand All @@ -97,32 +113,35 @@ public SpacesItemDecoration(Context context, int orientation, int headerNoShowSi
*
* @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
*/
public void setOrientation(int orientation) {
public SpacesItemDecoration setOrientation(int orientation) {
if (orientation != HORIZONTAL && orientation != VERTICAL) {
throw new IllegalArgumentException("Invalid orientation. It should be either HORIZONTAL or VERTICAL");
}
mOrientation = orientation;
return this;
}

/**
* Sets the {@link Drawable} for this divider.
*
* @param drawable Drawable that should be used as a divider.
*/
public void setDrawable(Drawable drawable) {
public SpacesItemDecoration setDrawable(Drawable drawable) {
if (drawable == null) {
throw new IllegalArgumentException("drawable cannot be null.");
}
mDivider = drawable;
return this;
}

public void setDrawable(@DrawableRes int id) {
public SpacesItemDecoration setDrawable(@DrawableRes int id) {
setDrawable(ContextCompat.getDrawable(mContext, id));
return this;
}

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
if (parent.getLayoutManager() == null || mDivider == null) {
if (parent.getLayoutManager() == null || (mDivider == null && mPaint == null)) {
return;
}
if (mOrientation == VERTICAL) {
Expand All @@ -140,8 +159,7 @@ private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State
if (parent.getClipToPadding()) {
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
canvas.clipRect(left, parent.getPaddingTop(), right,
parent.getHeight() - parent.getPaddingBottom());
canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
} else {
left = 0;
right = parent.getWidth();
Expand All @@ -159,11 +177,22 @@ private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State
}
// 过滤到尾部不显示的分割线
if (childRealPosition <= lastPosition - mFooterNoShowSize) {
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
if (mDivider != null) {
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}

if (mPaint != null) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left1 = left + mLeftTopPadding;
int right1 = right - mRightBottomPadding;
int top1 = child.getBottom() + params.bottomMargin;
int bottom1 = top1 + mDividerSpacing;
canvas.drawRect(left1, top1, right1, bottom1, mPaint);
}
}
}
canvas.restore();
Expand Down Expand Up @@ -196,19 +225,30 @@ private void drawHorizontal(Canvas canvas, RecyclerView parent, RecyclerView.Sta
}
// 过滤到尾部不显示的分割线
if (childRealPosition <= lastPosition - mFooterNoShowSize) {
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int right = mBounds.right + Math.round(child.getTranslationX());
final int left = right - mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
if (mDivider != null) {
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int right = mBounds.right + Math.round(child.getTranslationX());
final int left = right - mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}

if (mPaint != null) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left1 = child.getRight() + params.rightMargin;
int right1 = left1 + mDividerSpacing;
int top1 = top + mLeftTopPadding;
int bottom1 = bottom - mRightBottomPadding;
canvas.drawRect(left1, top1, right1, bottom1, mPaint);
}
}
}
canvas.restore();
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mDivider == null) {
if (mDivider == null && mPaint == null) {
outRect.set(0, 0, 0, 0);
return;
}
Expand All @@ -217,15 +257,15 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle
int lastPosition = state.getItemCount() - 1;
int position = parent.getChildAdapterPosition(view);
if (position <= lastPosition - mFooterNoShowSize) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
outRect.set(0, 0, 0, mDivider != null ? mDivider.getIntrinsicHeight() : mDividerSpacing);
} else {
outRect.set(0, 0, 0, 0);
}
} else {
int lastPosition = state.getItemCount() - 1;
int position = parent.getChildAdapterPosition(view);
if (position <= lastPosition - mFooterNoShowSize) {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
outRect.set(0, 0, mDivider != null ? mDivider.getIntrinsicWidth() : mDividerSpacing, 0);
} else {
outRect.set(0, 0, 0, 0);
}
Expand All @@ -238,18 +278,54 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle
* @param headerNoShowSize 头部 不显示分割线的item个数
* @param footerNoShowSize 尾部 不显示分割线的item个数,默认1,不显示最后一个,最后一个一般为加载更多view
*/
public void setNoShowDivider(int headerNoShowSize, int footerNoShowSize) {
public SpacesItemDecoration setNoShowDivider(int headerNoShowSize, int footerNoShowSize) {
this.mHeaderNoShowSize = headerNoShowSize;
this.mFooterNoShowSize = footerNoShowSize;
return this;
}

/**
* 设置不显示头部分割线的item个数
*
* @param headerNoShowSize 头部 不显示分割线的item个数
*/
public void setHeaderNoShowDivider(int headerNoShowSize) {
public SpacesItemDecoration setHeaderNoShowDivider(int headerNoShowSize) {
this.mHeaderNoShowSize = headerNoShowSize;
return this;
}

public SpacesItemDecoration setParam(int dividerColor, int dividerSpacing) {
return setParam(dividerColor, dividerSpacing, 0, 0);
}

/**
* 直接设置分割线颜色等,不设置drawable
*
* @param dividerColor 分割线颜色
* @param dividerSpacing 分割线间距
* @param leftTopPaddingDp 如果是横向 - 左边距
* 如果是纵向 - 上边距
* @param rightBottomPaddingDp 如果是横向 - 右边距
* 如果是纵向 - 下边距
*/
public SpacesItemDecoration setParam(int dividerColor, int dividerSpacing, float leftTopPaddingDp, float rightBottomPaddingDp) {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(ContextCompat.getColor(mContext, dividerColor));
mDividerSpacing = dividerSpacing;
mLeftTopPadding = dip2px(leftTopPaddingDp);
mRightBottomPadding = dip2px(rightBottomPaddingDp);
mDivider = null;
return this;
}

/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dip2px(float dpValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ private void initAdapter() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
binding.recyclerView.setLayoutManager(layoutManager);
SpacesItemDecoration itemDecoration = new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL, 1);
itemDecoration.setDrawable(ContextCompat.getDrawable(binding.recyclerView.getContext(), R.drawable.shape_line));
binding.recyclerView.addItemDecoration(itemDecoration);
binding.recyclerView.addItemDecoration(new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL).setDrawable(R.drawable.shape_line));
binding.recyclerView.setAdapter(mAdapter);
binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ private void initAdapter() {
layoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
// 加了分割线,滚动条才会置顶
recyclerView.addItemDecoration(new SpacesItemDecoration(recyclerView.getContext(), SpacesItemDecoration.VERTICAL, 1));
SpacesItemDecoration itemDecoration = new SpacesItemDecoration(recyclerView.getContext(), SpacesItemDecoration.VERTICAL, 1);
recyclerView.addItemDecoration(itemDecoration.setParam(R.color.colorBlue, 10, 10, 10));
recyclerView.setAdapter(mAdapter);
recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
@Override
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/drawable/shape_line_custom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<item
android:gravity="center"
android:left="20dp">
<shape>

<size android:height="1dp" />

<corners android:radius="1dp" />

<solid android:color="@color/colorPrimary" />

</shape>
</item>

</layer-list>

0 comments on commit 4654bdd

Please sign in to comment.