0

I am trying to create an ItemDecoration drawn as a divider between a RecyclerView items with left padding.

Currently I have this implementation inside ItemDecoration class:

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    final int childCount = parent.getChildCount();

    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

        final int top = lm.getDecoratedBottom(child);
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left + 40, top, right, bottom);
        mDivider.draw(c);
    }
}

But it seems like the divider is still drawing itself full width. So what am I doing wrong?

I also tried setting an InsetDrawable with left insets (from xml), as the divider, but it seems like it will not draw it at all.

PS. That 40 value is just a hardcoded one, for explanatory purpose. mDivider is a Drawable.

DoruAdryan
  • 1,314
  • 3
  • 20
  • 35

1 Answers1

0

You can follow this tutorial or just read this answer to a similar question.

tip:

For just add left padding:

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.left = 30;
}
Community
  • 1
  • 1
MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72