Skip to content

Instantly share code, notes, and snippets.

@dohaivu
Last active September 18, 2017 03:38
Show Gist options
  • Save dohaivu/7b0ea982209dc746f4aebbd6c19cf37f to your computer and use it in GitHub Desktop.
Save dohaivu/7b0ea982209dc746f4aebbd6c19cf37f to your computer and use it in GitHub Desktop.
RecyclerView #android
protected void onCreate(Bundle savedInstanceState) {
DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext(), VERTICAL);
recyclerView.addItemDecoration(decoration);
}
// override onDraw ItemDecoration.java, draw a devider line
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
canvas.save();
final int leftWithMargin = convertDpToPixel(56);
final int right = parent.getWidth();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
int adapterPosition = parent.getChildAdapterPosition(child);
left = (adapterPosition == lastPosition) ? 0 : leftWithMargin;
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
canvas.restore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment