Grid分割线
public class DividerGridItemDecoration extends RecyclerView .ItemDecoration {
private static final int [] ATTRS = new int [] {
android.R.attr.listDivider
};
private Drawable mDivider;
public DividerGridItemDecoration (Context context) {
this (context,null );
}
public DividerGridItemDecoration (Context context,@DrawableRes int resId) {
this (context, ContextCompat.getDrawable(context,resId));
}
public DividerGridItemDecoration (Context context,Drawable divider) {
if (divider!=null ){
mDivider = divider;
}else {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0 );
a.recycle();
}
}
@Override
public void onDraw (Canvas c, RecyclerView parent, State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
private int getSpanCount (RecyclerView parent) {
int spanCount = -1 ;
LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager)
.getSpanCount();
}
return spanCount;
}
public void drawHorizontal (Canvas c, RecyclerView parent) {
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.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin
+ mDivider.getIntrinsicWidth();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawVertical (Canvas c, RecyclerView parent) {
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.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private boolean isLastColum (RecyclerView parent, int pos, int spanCount,int childCount) {
LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((pos + 1 ) % spanCount == 0 ){
return true ;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((pos + 1 ) % spanCount == 0 ){
return true ;
}
} else {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)
return true ;
}
}
return false ;
}
private boolean isLastRaw (RecyclerView parent, int pos, int spanCount, int childCount) {
LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)
return true ;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)
return true ;
} else {
if ((pos + 1 ) % spanCount == 0 ) {
return true ;
}
}
}
return false ;
}
@Override
public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super .getItemOffsets(outRect, view, parent, state);
int spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
if (isLastRaw(parent,position , spanCount, childCount)){
outRect.set(0 , 0 , mDivider.getIntrinsicWidth(), 0 );
} else if (isLastColum(parent,position, spanCount, childCount)){
outRect.set(0 , 0 , 0 , mDivider.getIntrinsicHeight());
} else {
outRect.set(0 , 0 , mDivider.getIntrinsicWidth(),
mDivider.getIntrinsicHeight());
}
}
}
Linear分割线
public class DividerItemDecoration extends RecyclerView .ItemDecoration {
private static final int [] ATTRS = new int []{
android.R.attr.listDivider
};
public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL = LinearLayoutManager.VERTICAL;
private final Rect mBounds = new Rect();
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration (Context context, int orientation) {
this (context,orientation,null );
}
public DividerItemDecoration (Context context, int orientation,@DrawableRes int resId) {
this (context,orientation, ContextCompat.getDrawable(context,resId));
}
public DividerItemDecoration (Context context, int orientation,Drawable divider) {
if (divider!=null ){
mDivider = divider;
}else {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0 );
a.recycle();
}
setOrientation(orientation);
}
public void setOrientation (int orientation) {
if (orientation != HORIZONTAL && orientation != VERTICAL) {
throw new IllegalArgumentException(
"Invalid orientation. It should be either HORIZONTAL or VERTICAL" );
}
mOrientation = orientation;
}
public void setDrawable (@NonNull Drawable drawable) {
if (drawable == null ) {
throw new IllegalArgumentException("Drawable cannot be null." );
}
mDivider = drawable;
}
@Override
public void onDraw (Canvas c, RecyclerView parent, RecyclerView.State state) {
if (parent.getLayoutManager() == null ) {
return ;
}
if (mOrientation == VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
@SuppressLint ("NewApi" )
private void drawVertical (Canvas canvas, RecyclerView parent) {
canvas.save();
final int left;
final int right;
if (parent.getClipToPadding()) {
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
canvas.clipRect(left, parent.getPaddingTop(), right,
parent.getHeight() - parent.getPaddingBottom());
} else {
left = 0 ;
right = parent.getWidth();
}
final int childCount = parent.getChildCount();
for (int i = 0 ; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
canvas.restore();
}
@SuppressLint ("NewApi" )
private void drawHorizontal (Canvas canvas, RecyclerView parent) {
canvas.save();
final int top;
final int bottom;
if (parent.getClipToPadding()) {
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
canvas.clipRect(parent.getPaddingLeft(), top,
parent.getWidth() - parent.getPaddingRight(), bottom);
} else {
top = 0 ;
bottom = parent.getHeight();
}
final int childCount = parent.getChildCount();
for (int i = 0 ; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
final int left = right - mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
canvas.restore();
}
@Override
public void getItemOffsets (Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
if (mOrientation == VERTICAL) {
outRect.set(0 , 0 , 0 , mDivider.getIntrinsicHeight());
} else {
outRect.set(0 , 0 , mDivider.getIntrinsicWidth(), 0 );
}
}
}