Created
June 2, 2017 02:48
-
-
Save samuel22gj/114842b23006c8b914454e88d146263b to your computer and use it in GitHub Desktop.
A kind of ItemDecoration to draw gridlines with specific color and size.
This file contains hidden or 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
| /** | |
| * A kind of {@link android.support.v7.widget.RecyclerView.ItemDecoration} to draw gridlines with | |
| * specific color and size. It assumes every item at same column has the same width and don't draw border. | |
| */ | |
| public class GridlinesItemDecoration extends RecyclerView.ItemDecoration { | |
| private static final String TAG = GridlinesItemDecoration.class.getSimpleName(); | |
| private int mDividerSize; | |
| private Paint mPaint; | |
| private final Rect mBounds = new Rect(); | |
| public GridlinesItemDecoration(@ColorInt int color, @IntRange(from = 0) int size) { | |
| mDividerSize = size; | |
| mPaint = new Paint(); | |
| mPaint.setColor(color); | |
| mPaint.setStrokeWidth(mDividerSize); | |
| } | |
| @Override | |
| public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) { | |
| final int padding = mDividerSize / 2; | |
| outRect.set(padding, padding, padding, padding); | |
| } | |
| @Override | |
| public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { | |
| canvas.save(); | |
| // Get boundary of parent. | |
| final int top = 0; | |
| final int bottom = parent.getHeight(); | |
| final int left = 0; | |
| final int right = parent.getWidth(); | |
| int columnCount = 0; | |
| final int childCount = parent.getChildCount(); | |
| // Loop first column to draw vertical divider. | |
| int x = 0; | |
| for (int i = 0; i < childCount; i++) { | |
| columnCount++; | |
| // Get width of child view. | |
| final View child = parent.getChildAt(i); | |
| parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds); | |
| final int width = mBounds.right - mBounds.left; | |
| // Draw vertical divider if space enough. | |
| x += width + mDividerSize; | |
| if (x < right) { | |
| canvas.drawLine(x, top, x, bottom, mPaint); | |
| } else { | |
| break; | |
| } | |
| } | |
| // Loop every row to draw horizontal divider. | |
| int y = 0; | |
| for (int i = 0; i < childCount; i += columnCount) { | |
| // Get height of child view. | |
| final View child = parent.getChildAt(i); | |
| parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds); | |
| final int height = mBounds.bottom - mBounds.top; | |
| // Draw horizontal divider if space enough. | |
| y += height + mDividerSize; | |
| if (y < bottom) { | |
| canvas.drawLine(left, y, right, y, mPaint); | |
| } else { | |
| break; | |
| } | |
| } | |
| canvas.restore(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment