Android RecyclerView 设置分割线(item头部也绘制)

时间:2025-03-24 20:32:02
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[]{}; private Drawable mDivider; public DividerGridItemDecoration(Context context) { final TypedArray a = (ATTRS); mDivider = (0); (); } @Override public void onDraw(Canvas c, RecyclerView parent, state) { drawHorizontal(c, parent); drawVertical(c, parent); } private int getSpanCount(RecyclerView parent) { // 列数 int spanCount = -1; layoutManager = (); 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 = (); int column_num = getSpanCount(parent); for (int i = 0; i < childCount; i++) { final View child = (i); final params = () child .getLayoutParams(); final int left = () - ; final int right = () + + (); // final int top = () + ; // final int bottom = top + (); // (left, top, right, bottom); // (c); int top = 0; int bottom = 0; if ((i / column_num) == 0) { //画item最上面的分割线 top = 0; bottom = top + (); (left, top, right, bottom); (c); //画item下面的分割线 top = () + ; bottom = top + (); (left, top, right, bottom); (c); } else { //画item下面的分割线 top = () + ; bottom = top + (); (left, top, right, bottom); (c); } } } public void drawVertical(Canvas c, RecyclerView parent) { final int childCount = (); int column_num = getSpanCount(parent); for (int i = 0; i < childCount; i++) { final View child = (i); final params = () child .getLayoutParams(); final int top = () - ; final int bottom = () + ; // final int left = () + ; // final int right = left + (); // (left, top, right, bottom); // (c); int left = 0; int right = 0; if ((i % column_num) == 0) { //item左边分割线 left = 0; right = left + (); (left, top, right, bottom); (c); //item右边分割线 left = () + ; right = left + (); } else { left = () + ; right = left + (); } (left, top, right, bottom); (c); } } private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) { layoutManager = (); if (layoutManager instanceof GridLayoutManager) { // // 如果是最后一列,则不需要绘制右边 // if ((pos + 1) % spanCount == 0) { // return true; // } } else if (layoutManager instanceof StaggeredGridLayoutManager) { int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); if (orientation == ) { // 如果是最后一列,则不需要绘制右边 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 = (); if (layoutManager instanceof GridLayoutManager) { // childCount = childCount - childCount % spanCount; // if (pos >= childCount)// 如果是最后一行,则不需要绘制底部 // return true; } else if (layoutManager instanceof StaggeredGridLayoutManager) { int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); // StaggeredGridLayoutManager 且纵向滚动 if (orientation == ) { childCount = childCount - childCount % spanCount; if (pos >= childCount)// 如果是最后一行,则不需要绘制底部 return true; // StaggeredGridLayoutManager 且横向滚动 } else { // 如果是最后一行,则不需要绘制底部 if ((pos + 1) % spanCount == 0) { return true; } } } return false; } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { int spanCount = getSpanCount(parent); int childCount = ().getItemCount(); // 如果是最后一行,则不需要绘制底部 if (isLastRaw(parent, itemPosition, spanCount, childCount)) { (0, 0, (), 0); // 如果是最后一列,则不需要绘制右边 } else if (isLastColum(parent, itemPosition, spanCount, childCount)) { (0, 0, 0, ()); } else { (0, 0, (), ()); } } }