Casper Lindberg
Casper Lindberg

Reputation: 1103

Android - RecyclerView list equal spacing between items and parent

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.

Upvotes: 2

Views: 3568

Answers (2)

SpiritCrusher
SpiritCrusher

Reputation: 21043

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 ..

Upvotes: 6

Ravi
Ravi

Reputation: 2367

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".

Upvotes: 1

Related Questions