3

I have a RecyclerView and have added an ItemDecoration to it as follows :-

mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

My ItemDecoration looks like :-

public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
    {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
}

This draws a divider taking full width of the screen. I want to add a little margin from the left and right of about 15dp (red line) so that it looks shorter than the screen width something like below :-

enter image description here

How can I achieve this? Any suggestions are appreciated.

user3034944
  • 1,541
  • 2
  • 17
  • 35

2 Answers2

0

Have a look at getItemOffsets method. Something like following should do it.

override fun getItemOffsets(
    rect: Rect,
    view: View,
    parent: RecyclerView,
    state: RecyclerView.State
) {
    rect.right = parent.right - yourMargin
    rect.left = parent.left + yourMargin
}

This method will set the rect space where you'll draw (onDraw(...)) your decoration.

Nicolas Duponchel
  • 1,219
  • 10
  • 17
0

Just put the value of

    final int left = 200;
    final int right= 200;
Triaro
  • 90
  • 1
  • 7
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – β.εηοιτ.βε Jun 21 '20 at 18:25
  • @β.εηοιτ.βε In this case I didn't find anything much to explain but I will definitely take care of this in future. – Triaro Jun 25 '20 at 07:16