Last active
December 23, 2016 08:53
-
-
Save zty5678/4d33ace9d563a6b72e97f0b384f4064f to your computer and use it in GitHub Desktop.
RecyclerView grid layout divider. Partly from https://github.com/bignerdranch/simple-item-decoration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Canvas; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
/** | |
* Adds interior dividers to a RecyclerView with a GridLayoutManager. | |
*/ | |
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration { | |
private Drawable mHorizontalDivider; | |
private Drawable mVerticalDivider; | |
private int mNumColumns; | |
public GridDividerItemDecoration(Drawable horizontalDivider, Drawable verticalDivider, int numColumns) { | |
mHorizontalDivider = horizontalDivider; | |
mVerticalDivider = verticalDivider; | |
mNumColumns = numColumns; | |
} | |
@Override | |
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { | |
drawHorizontalDividers(canvas, parent, state); | |
drawVerticalDividers(canvas, parent, state); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
super.getItemOffsets(outRect, view, parent, state); | |
boolean childIsInLeftmostColumn = (parent.getChildAdapterPosition(view) % mNumColumns) == 0; | |
if (childIsInLeftmostColumn) { | |
outRect.left = mHorizontalDivider.getIntrinsicWidth(); | |
outRect.right = mHorizontalDivider.getIntrinsicWidth(); | |
} else { | |
outRect.right = mHorizontalDivider.getIntrinsicWidth(); | |
} | |
boolean childIsInFirstRow = (parent.getChildAdapterPosition(view)) < mNumColumns; | |
if (childIsInFirstRow) { | |
outRect.top = mVerticalDivider.getIntrinsicHeight(); | |
outRect.bottom = mVerticalDivider.getIntrinsicHeight();//+outRect.height(); | |
} else { | |
outRect.bottom = mVerticalDivider.getIntrinsicHeight();//+outRect.height(); | |
} | |
} | |
private void drawHorizontalDividers(Canvas canvas, RecyclerView parent, RecyclerView.State state) { | |
int childCount = parent.getChildCount(); | |
int rowCount = childCount % mNumColumns == 0 ? childCount / mNumColumns : (childCount / mNumColumns + 1); | |
int lastRowChildCount = childCount % mNumColumns == 0 ? mNumColumns : childCount % mNumColumns; | |
if (childCount > 0) { | |
for (int i = 0; i <= mNumColumns; i++) { | |
int firstRowChildIndex = i; | |
int lastRowChildIndex; | |
if (i < lastRowChildCount) { | |
lastRowChildIndex = i + (rowCount - 1) * mNumColumns; | |
} else { | |
lastRowChildIndex = i + (rowCount - 2) * mNumColumns; | |
} | |
if (firstRowChildIndex < childCount) { | |
View firstRowChild = parent.getChildAt(firstRowChildIndex); | |
View lastRowChild = parent.getChildAt(lastRowChildIndex); | |
if (i == 0) { | |
{ | |
int dividerTop = firstRowChild.getTop(); | |
int dividerRight = firstRowChild.getLeft(); | |
int dividerLeft = dividerRight - mHorizontalDivider.getIntrinsicWidth(); | |
int dividerBottom = lastRowChild.getBottom(); | |
mHorizontalDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
mHorizontalDivider.draw(canvas); | |
} | |
{ | |
int dividerTop = firstRowChild.getTop(); | |
int dividerRight = firstRowChild.getRight() + mHorizontalDivider.getIntrinsicWidth(); | |
int dividerLeft = firstRowChild.getRight(); | |
int dividerBottom = lastRowChild.getBottom(); | |
mHorizontalDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
mHorizontalDivider.draw(canvas); | |
} | |
} else { | |
int dividerTop = firstRowChild.getTop(); | |
int dividerRight = firstRowChild.getRight() + mHorizontalDivider.getIntrinsicWidth(); | |
int dividerLeft = firstRowChild.getRight(); | |
int dividerBottom = lastRowChild.getBottom(); | |
mHorizontalDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
mHorizontalDivider.draw(canvas); | |
} | |
} | |
} | |
} | |
} | |
private void drawVerticalDividers(Canvas canvas, RecyclerView parent, RecyclerView.State state) { | |
int childCount = parent.getChildCount(); | |
int rowCount = childCount % mNumColumns == 0 ? childCount / mNumColumns : (childCount / mNumColumns + 1); | |
int rightmostChildIndex; | |
int leftmostChildIndex; | |
for (int i = 0; i <= rowCount; i++) { | |
if (i == rowCount) { | |
leftmostChildIndex = (i - 1) * mNumColumns; | |
rightmostChildIndex = parent.getChildCount() - 1; | |
} else { | |
leftmostChildIndex = i * mNumColumns; | |
rightmostChildIndex = (i * mNumColumns) + mNumColumns - 1; | |
if (rightmostChildIndex >= childCount) { | |
rightmostChildIndex = childCount - 1; | |
} | |
} | |
View leftmostChild = parent.getChildAt(leftmostChildIndex); | |
View rightmostChild = parent.getChildAt(rightmostChildIndex); | |
if (i == 0) { | |
{ | |
int dividerLeft = leftmostChild.getLeft() - mVerticalDivider.getIntrinsicWidth(); | |
int dividerBottom = leftmostChild.getTop(); | |
int dividerTop = leftmostChild.getTop() - mVerticalDivider.getIntrinsicHeight(); | |
int dividerRight = rightmostChild.getRight() + mVerticalDivider.getIntrinsicWidth(); | |
mVerticalDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
mVerticalDivider.draw(canvas); | |
} | |
{ | |
int dividerLeft = leftmostChild.getLeft() - mVerticalDivider.getIntrinsicWidth(); | |
int dividerBottom = leftmostChild.getBottom() + mVerticalDivider.getIntrinsicHeight(); | |
int dividerTop = leftmostChild.getBottom(); | |
int dividerRight = rightmostChild.getRight() + mVerticalDivider.getIntrinsicWidth(); | |
mVerticalDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
mVerticalDivider.draw(canvas); | |
} | |
} else { | |
int dividerLeft = leftmostChild.getLeft() - mVerticalDivider.getIntrinsicWidth(); | |
int dividerBottom = leftmostChild.getBottom() + mVerticalDivider.getIntrinsicHeight(); | |
int dividerTop = leftmostChild.getBottom(); | |
int dividerRight = rightmostChild.getRight() + mVerticalDivider.getIntrinsicWidth(); | |
mVerticalDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
mVerticalDivider.draw(canvas); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment