Created
April 23, 2015 19:18
-
-
Save Groxx/0bb754807f739d44199c to your computer and use it in GitHub Desktop.
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
public class GridDividers extends RecyclerView.ItemDecoration { | |
private final Rect viewRect = new Rect(); | |
private final Paint stroke = new Paint(); | |
private final int padding; | |
/** | |
* @param parentPadding should be 1/2 the padding desired between views, as views will be surrounded by the | |
* passed padding, causing it to double up on all edges. This is also used to determine | |
* where to draw a fake padding border to hide our trickery. | |
* @param backgroundColor background color used by the parent view(s), used to draw a border to look like normal | |
* RecyclerView padding. | |
*/ | |
public GridDividers(final int parentPadding, final int backgroundColor) { | |
this.padding = parentPadding; | |
stroke.setStrokeWidth(parentPadding * 2); | |
stroke.setColor(backgroundColor); | |
stroke.setStyle(Paint.Style.STROKE); | |
} | |
@Override | |
public void getItemOffsets( | |
final Rect outRect, | |
final View view, | |
final RecyclerView parent, | |
final RecyclerView.State state) { | |
// pad the views so the background shows through | |
outRect.set(padding, padding, padding, padding); | |
} | |
@Override | |
public void onDrawOver(final Canvas c, final RecyclerView parent, final RecyclerView.State state) { | |
// draw a solid border around the canvas which ends up looking like "normal" recyclerview padding, clipping | |
// the children as they scroll past. | |
viewRect.set(0, 0, c.getWidth(), c.getHeight()); | |
viewRect.inset(padding, padding); | |
c.drawRect(viewRect, stroke); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment