Skip to content

Instantly share code, notes, and snippets.

@talosdev
Last active October 9, 2016 14:17
Show Gist options
  • Save talosdev/07747f1281aeee1f317663d11e7a0f96 to your computer and use it in GitHub Desktop.
Save talosdev/07747f1281aeee1f317663d11e7a0f96 to your computer and use it in GitHub Desktop.
RecyclerView dividers with ItemDecoration
final Drawable divider = ContextCompat.getDrawable(context, R.drawable.last_events_divider);
previousEventsRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
/**
* Draws a divider (1dp-line) inside (over) the child.
* Applies to all elements except the first one.
* @param c
* @param parent
* @param state
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 1; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int dividerTop = child.getTop() - params.topMargin;
int dividerBottom = dividerTop + divider.getIntrinsicHeight();
divider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
divider.draw(c);
}
}
});
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:height="1dp" />
<solid android:color="@color/isabelline" />
</shape>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment