Three methods to solve the problem that the dividing line of RecycleView is not centered
- 2021-07-18 09:05:44
- OfStack
In this paper, we share three solutions to the problem that the RecycleView dividing line is not centered for your reference. The specific contents are as follows
Method 1:
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int mSpace;
private int mSpanCount; // RecyclerView How many columns are there
private boolean mHasPadding; // RecyclerView Whether there is Padding
public SpacesItemDecoration(int mSpace) {
this.mSpace = mSpace;
this.mHasPadding = true;
}
public SpacesItemDecoration(int mSpace, boolean hasPadding) {
this.mSpace = mSpace;
this.mHasPadding = hasPadding;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
// Number of initialized columns
if (mSpanCount == 0) {
this.mSpanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
}
int position = parent.getChildAdapterPosition(view); // item position
int column = position % mSpanCount; // item column
if (mHasPadding) {
outRect.left = mSpace - column * mSpace / mSpanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * mSpace / mSpanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < mSpanCount) { // top edge
outRect.top = mSpace;
}
outRect.bottom = mSpace; // item bottom
} else {
outRect.left = column * mSpace / mSpanCount; // column * ((1f / spanCount) * spacing)
outRect.right = mSpace - (column + 1) * mSpace / mSpanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= mSpanCount) {
outRect.top = mSpace; // item top
}
}
}
public void setHasPadding(boolean hasPadding) {
this.mHasPadding = hasPadding;
}
}
Method 2:
public class MutiItemDecoration extends RecyclerView.ItemDecoration {
public enum Type {
VERTICAL, HORIZONTAL, ALL
}
private Type type;// Type of dividing line
private int dividerSize = 10;// Dimension of dividing line
public MutiItemDecoration(MutiItemDecoration.Type type, int dividerSize) {
this.type = type;
this.dividerSize = dividerSize;
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
int spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
switch (type) {
case ALL:
if (itemPosition % spanCount == 0) {// No. 1 1 Column
if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
outRect.set(0, 0, dividerSize / 2, 0);
} else {
outRect.set(0, 0, dividerSize / 2, dividerSize);
}
} else if (itemPosition % spanCount == spanCount - 1) {// Finally 1 Column
if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
outRect.set(dividerSize / 2, 0, 0, 0);
} else {
outRect.set(dividerSize / 2, 0, 0, dividerSize);
}
} else {// Intermediate column
if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
outRect.set(dividerSize / 2, 0, dividerSize / 2, 0);
} else {
outRect.set(dividerSize / 2, 0, dividerSize / 2, dividerSize);
}
}
break;
case VERTICAL:
if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
outRect.set(0, 0, 0, 0);
} else {
outRect.set(0, 0, 0, dividerSize);
}
break;
case HORIZONTAL:
if (isLastColum(parent, itemPosition, spanCount, childCount)) {
outRect.set(0, 0, 0, 0);
} else {
outRect.set(0, 0, dividerSize, 0);
}
break;
}
}
// Is it the last 1 Column
private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((pos + 1) % spanCount == 0)
return true;
} else {
if (pos == childCount - 1)
return true;
}
return false;
}
// Is it the last 1 Row
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)
return true;
} else {
if (pos == childCount - 1)
return true;
}
return false;
}
// Returns the number of columns
private int getSpanCount(RecyclerView parent) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
return ((GridLayoutManager) layoutManager).getSpanCount();
}
return -1;
}
}
Method 3: (currently only 2 columns are supported)
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable mDivider;
public DividerGridItemDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = ContextCompat.getDrawable(context, R.drawable.shape_divider);
a.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
private int getSpanCount(RecyclerView parent) {
// Number of columns
int spanCount = -1;
RecyclerView.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();
if (i % 2 == 1) {
final int top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
final int left = child.getLeft() - params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth() / 2;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
} else {
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() / 2;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
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) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((pos + 1) % spanCount == 0)// If it is the last 1 Column, you do not need to draw the right side
{
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((pos + 1) % spanCount == 0)// If it is the last 1 Column, you do not need to draw the right side
{
return true;
}
} else {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)// If it is the last 1 Column, you do not need to draw the right side
return true;
}
}
return false;
}
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)// If it is the last 1 Row, you do not need to draw the bottom
return true;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
// StaggeredGridLayoutManager And scroll vertically
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = childCount - childCount % spanCount;
// If it is the last 1 Row, you do not need to draw the bottom
if (pos >= childCount)
return true;
} else
// StaggeredGridLayoutManager And scroll horizontally
{
// If it is the last 1 Row, you do not 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 = parent.getAdapter().getItemCount();
if (isLastRaw(parent, itemPosition, spanCount, childCount))// If it is the last 1 Row, you do not need to draw the bottom
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
} else if (isLastColum(parent, itemPosition, spanCount, childCount))// If it is the last 1 Column, you do not need to draw the right side
{
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(),
mDivider.getIntrinsicHeight());
}
}
}