2

I have trouble figuring out how to set the padding and margin for my recycler view in the XML file to achieve this behaviour maintaining equal spacing between recycler list items and the same spacing to the parent.

ParenStart <-10dp-> ListItem <-10dp-> ListItem <-10dp-> ParentEnd

And at the same time be able to scroll full width of the screen (parent).

enter image description here

The trouble is that this behaviour occurs if I set a padding on the parent and the list items, like this: ParenStart <-12.5dp-> ListItem <-5dp-> ListItem <-12.5dp-> ParentEnd

enter image description here

And if I do not set padding on the parent and only set padding on the list items I get the following trouble ParenStart <-10dp-> ListItem <-20dp-> ListItem <-10dp-> ParentEnd

Restating my goal: ParenStart <-10dp-> ListItem <-10dp-> ListItem <-10dp-> ParentEnd And at the same time be able to scroll full width of the parent.

Casper Lindberg
  • 1,053
  • 1
  • 11
  • 16
  • Instead of giving padding to the entire parent view, you can try to give padding to all views inside parent view except the recyclerview – Parag Pawar Apr 10 '20 at 04:53

2 Answers2

4

You can use clipToPadding attribute in RecyclerView.

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingStart="10dp"
             />

and then use only right margin in item layout. Then arrangement will be like
ParentStart(clippadding) <10dp> ListItem <10dp> ListItem <10dp> ParentEnd.

OR you can try using DividerItemDecoration ..

ADM
  • 20,406
  • 11
  • 52
  • 83
0

Add the following clipToPadding in recyclerview

 <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_tpf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:paddingStart="10dp" />

Add android:clipToPadding="false".

Ravi
  • 2,277
  • 3
  • 22
  • 37