Skip to content

Instantly share code, notes, and snippets.

@dmitriy-chernysh
Last active September 24, 2016 13:22
Show Gist options
  • Save dmitriy-chernysh/a2e0401bd17511cd5d32aeb4a37d6429 to your computer and use it in GitHub Desktop.
Save dmitriy-chernysh/a2e0401bd17511cd5d32aeb4a37d6429 to your computer and use it in GitHub Desktop.
/**
* Divider for RecyclerView list items
* <p>
* Created by Dmitriy V. Chernysh on 24.09.16.
* [email protected]
* <p>
* www.mobile-dev.pro
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDrawableDivider;
public DividerItemDecoration(Drawable drawableDivider) {
mDrawableDivider = drawableDivider;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (parent.getChildAdapterPosition(view) == 0) {
return;
}
outRect.top = mDrawableDivider.getIntrinsicHeight();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
Log.d(Constants.LOG_TAG_DEBUG, "DividerItemDecoration.onDraw(): ");
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int dividerTop = child.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + mDrawableDivider.getIntrinsicHeight();
mDrawableDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
mDrawableDivider.draw(c);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment