Skip to content

Instantly share code, notes, and snippets.

@ThomasStuetz
Created May 13, 2017 17:00
Show Gist options
  • Save ThomasStuetz/109991a9f09c531e6a3d7d409a7ce92c to your computer and use it in GitHub Desktop.
Save ThomasStuetz/109991a9f09c531e6a3d7d409a7ce92c to your computer and use it in GitHub Desktop.
Google's sample implementation of dividers displayed between the RecyclerView items
@Override
public void onDrawOver(Canvas c, RecyclerView parent,
RecyclerView.State state) {
super.onDrawOver(c, parent, state);
// calculate left/right x-coordinates for all dividers
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
// for every item but the last, draw a line below it
for (int i = 0; i < parent.getChildCount() - 1; ++i) {
View item = parent.getChildAt(i); // get ith list item
// calculate top/bottom y-coordinates for current divider
int top = item.getBottom() + ((RecyclerView.LayoutParams)
item.getLayoutParams()).bottomMargin;
int bottom = top + divider.getIntrinsicHeight();
// draw the divider with the calculated bounds
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment