This article shares three solutions to the RecycleView segmentation line not centered for your reference. The specific content is as follows
Method 1:
public class SpacesItemDecoration extends { private int mSpace; private int mSpanCount; // How many columns does RecyclerView have private boolean mHasPadding; // Does RecyclerView have Padding public SpacesItemDecoration(int mSpace) { = mSpace; = true; } public SpacesItemDecoration(int mSpace, boolean hasPadding) { = mSpace; = hasPadding; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, state) { // Initialize the number of columns if (mSpanCount == 0) { = ((GridLayoutManager) ()).getSpanCount(); } int position = (view); // item position int column = position % mSpanCount; // item column if (mHasPadding) { = mSpace - column * mSpace / mSpanCount; // spacing - column * ((1f / spanCount) * spacing) = (column + 1) * mSpace / mSpanCount; // (column + 1) * ((1f / spanCount) * spacing) if (position < mSpanCount) { // top edge = mSpace; } = mSpace; // item bottom } else { = column * mSpace / mSpanCount; // column * ((1f / spanCount) * spacing) = mSpace - (column + 1) * mSpace / mSpanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing) if (position >= mSpanCount) { = mSpace; // item top } } } public void setHasPadding(boolean hasPadding) { = hasPadding; } }
Method 2:
public class MutiItemDecoration extends { public enum Type { VERTICAL, HORIZONTAL, ALL } private Type type;//Split Line Type private int dividerSize = 10;//Segmentation line size public MutiItemDecoration( type, int dividerSize) { = type; = dividerSize; } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { int spanCount = getSpanCount(parent); int childCount = ().getItemCount(); switch (type) { case ALL: if (itemPosition % spanCount == 0) {//The first column if (isLastRaw(parent, itemPosition, spanCount, childCount)) { (0, 0, dividerSize / 2, 0); } else { (0, 0, dividerSize / 2, dividerSize); } } else if (itemPosition % spanCount == spanCount - 1) {//The last column if (isLastRaw(parent, itemPosition, spanCount, childCount)) { (dividerSize / 2, 0, 0, 0); } else { (dividerSize / 2, 0, 0, dividerSize); } } else {//Intermediate column if (isLastRaw(parent, itemPosition, spanCount, childCount)) { (dividerSize / 2, 0, dividerSize / 2, 0); } else { (dividerSize / 2, 0, dividerSize / 2, dividerSize); } } break; case VERTICAL: if (isLastRaw(parent, itemPosition, spanCount, childCount)) { (0, 0, 0, 0); } else { (0, 0, 0, dividerSize); } break; case HORIZONTAL: if (isLastColum(parent, itemPosition, spanCount, childCount)) { (0, 0, 0, 0); } else { (0, 0, dividerSize, 0); } break; } } // Is it the last column 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 (pos == childCount - 1) return true; } return false; } // Is it the last line 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 (pos == childCount - 1) return true; } return false; } //Return the number of columns private int getSpanCount(RecyclerView parent) { layoutManager = (); if (layoutManager instanceof GridLayoutManager) { return ((GridLayoutManager) layoutManager).getSpanCount(); } return -1; } }
Method 3:(Currently only supports 2 columns)
public class DividerGridItemDecoration extends { private static final int[] ATTRS = new int[]{}; private Drawable mDivider; public DividerGridItemDecoration(Context context) { final TypedArray a = (ATTRS); mDivider = (context, .shape_divider); (); } @Override public void onDraw(Canvas c, RecyclerView parent, state) { drawHorizontal(c, parent); drawVertical(c, parent); } private int getSpanCount(RecyclerView parent) { // Number of columns 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 = (); 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); } } public void drawVertical(Canvas c, RecyclerView parent) { final int childCount = (); for (int i = 0; i < childCount; i++) { final View child = (i); final params = () child .getLayoutParams(); if (i % 2 == 1) { final int top = () - ; final int bottom = () + ; final int left = () - ; final int right = left + () / 2; (left, top, right, bottom); (c); } else { final int top = () - ; final int bottom = () + ; final int left = () + ; final int right = left + () / 2; (left, top, right, bottom); (c); } final int top = () - ; final int bottom = () + ; final int left = () + ; final int 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)// If it is the last column, there is no need to draw the right side { return true; } } else if (layoutManager instanceof StaggeredGridLayoutManager) { int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); if (orientation == ) { if ((pos + 1) % spanCount == 0)// If it is the last column, there is no need to draw the right side { return true; } } else { childCount = childCount - childCount % spanCount; if (pos >= childCount)// If it is the last column, there is no need to draw the right side 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)// If it is the last line, there is no need to draw the bottom return true; } else if (layoutManager instanceof StaggeredGridLayoutManager) { int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); // StaggeredGridLayoutManager and scroll vertically if (orientation == ) { childCount = childCount - childCount % spanCount; // If it is the last line, there is no need to draw the bottom if (pos >= childCount) return true; } else // StaggeredGridLayoutManager and scroll horizontally { // If it is the last line, there is no need to draw the bottom 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))// If it is the last line, there is no need to draw the bottom { (0, 0, (), 0); } else if (isLastColum(parent, itemPosition, spanCount, childCount))// If it is the last column, there is no need to draw the right side { (0, 0, 0, ()); } else { (0, 0, (), ()); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.