package com.hjlm.sanfentianxia.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.hjlm.sanfentianxia.R;
import com.hjlm.sanfentianxia.util.MyUtil;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
public class RecyclerDecorationextends RecyclerView.ItemDecoration {
private static final int[]ATTRS =new int[]{android.R.attr.listDivider};
private DrawablemDivider;
private int mWidth, headSize;
private int color;
public RecyclerDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
mWidth = MyUtil.dip2px(context, 10);
color = context.getResources().getColor(R.color.color_grey_EEEEEE);
a.recycle();
}
//设置有几个头部(默认有一个)
public RecyclerDecoration(Context context, int size) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
mWidth = MyUtil.dip2px(context, 10);
color = context.getResources().getColor(R.color.color_grey_EEEEEE);
headSize = size;
a.recycle();
}
//设置颜色和宽度
public RecyclerDecoration(Context context, int mColor, int width) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
color = context.getResources().getColor(mColor);
mWidth = MyUtil.dip2px(context, width);
a.recycle();
}
public RecyclerDecoration(Context context, int mColor, int width, int size) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
color = context.getResources().getColor(mColor);
mWidth = MyUtil.dip2px(context, width);
headSize = size;
a.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
c.drawColor(color);
drawHorizontal(c, parent);
drawVertical(c, parent);
}
private int getSpanCount(RecyclerView parent) {
// 列数
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManagerinstanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
}else if (layoutManagerinstanceof 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 isFirstColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManagerinstanceof GridLayoutManager) {
if ((pos +headSize) % spanCount ==0)// 如果是第一列,则需要绘制左边
{
return true;
}
}else if (layoutManagerinstanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((pos +headSize) % spanCount ==0)// 如果是第一列,则不需要绘制右边
{
return true;
}
}else {
childCount = Math.min(spanCount, childCount);
if (pos <= childCount)// 如果是第一列,则不需要绘制右边
return true;
}
}
return false;
}
private boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManagerinstanceof GridLayoutManager) {
if ((pos +headSize) % spanCount ==0)// 如果是最后一列,则不需要绘制右边
{
return true;
}
}else if (layoutManagerinstanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((pos +headSize) % spanCount ==0)// 如果是最后一列,则不需要绘制右边
{
return true;
}
}else {
childCount = childCount - childCount % spanCount -headSize -1;
;
if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
return true;
}
}
return false;
}
private boolean isFirstRaw(RecyclerView parent, int pos, int spanCount,
int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManagerinstanceof GridLayoutManager) {
childCount = Math.min(spanCount, childCount);
if (pos <= childCount)// 如果是第一行,则不需要绘制底部
return true;
}else if (layoutManagerinstanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
// StaggeredGridLayoutManager 且纵向滚动
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = Math.min(spanCount, childCount);
// 如果是第一行,则不需要绘制底部
if (pos <= childCount)
return true;
}else
// StaggeredGridLayoutManager 且横向滚动
{
// 如果是第一行,则不需要绘制底部
if ((pos +1) % spanCount ==1) {
return true;
}
}
}
return false;
}
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManagerinstanceof GridLayoutManager) {
childCount = childCount - childCount % spanCount -headSize -1;
if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
return true;
}else if (layoutManagerinstanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
// StaggeredGridLayoutManager 且纵向滚动
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = childCount - childCount % spanCount -headSize -1;
// 如果是最后一行,则不需要绘制底部
if (pos >= childCount)
return true;
}else
// StaggeredGridLayoutManager 且横向滚动
{
// 如果是最后一行,则不需要绘制底部
if ((pos +1) % spanCount ==0) {
return true;
}
}
}
return false;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int spanCount = getSpanCount(parent);
int position=parent.getChildAdapterPosition(view);
if (position>headSize) {
int index;
if(parent.getLayoutManager()instanceof StaggeredGridLayoutManager){
StaggeredGridLayoutManager.LayoutParams params =
(StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
// 当前位置
index = params.getSpanIndex();
}else{
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
index = params.getSpanIndex();
}
int X = spanCount - index;
if (X == spanCount) {
// 最左边
outRect.right =mWidth;
outRect.left =mWidth;
}else if (X ==1) {
// 最右边
outRect.right =mWidth;
}else {
outRect.right =mWidth;
}
}
outRect.bottom =mWidth;
}
// @Override
// public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
// int spanCount = getSpanCount(parent);
// int childCount = parent.getAdapter().getItemCount();
// if (itemPosition > headSize) {
//// if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
//// //如果是最后一行,则不需要绘制底部
//// if (isFirstColumn(parent, itemPosition, spanCount, childCount)) {
//// outRect.set(mWidth, 0, mWidth, mWidth * 2);
//// } else {
//// outRect.set(0, 0, mWidth, mWidth * 2);
//// }
//// }else
//// if (isLastColumn(parent, itemPosition, spanCount, childCount)){
//// // 如果是最后一列,则不需要绘制右边
//// outRect.set(0, 0, 0, mDivider.getIntrinsicHeight() + pos);
//// }
// if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
// //如果是最后一行,则需要绘制底部
// if (isLastColumn(parent, itemPosition, spanCount, childCount)) {
// outRect.set(mWidth, mWidth, mWidth, mWidth);
// } else {
// outRect.set(mWidth, mWidth, 0, mWidth);
// }
// } else if (isLastColumn(parent, itemPosition, spanCount, childCount)) {
// // 如果是最后一列,则需要绘制右边
// outRect.set(mWidth, mWidth, mWidth, 0);
// } else {
// outRect.set(mWidth, mWidth, 0, 0);
// }
// }
//
//
// }
}