Mark Price Mark Price - 1 month ago 11
Android Question

RecyclerView random acts of padding

I'm trying to follow the tutorial at http://www.androidhive.info/2016/01/android-working-with-recycler-view/
to implement a recyclerView and everything has gone to plan so far except for this one thing.

After scrolling and the elements are recycled, they are recreated with loads of padding (as shown here https://youtu.be/jkGLLRnjtN0)

I have no idea why it's doing this, can anybody shed some light? Thanks

EDIT: my row layout xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">

<TextView
android:id="@+id/title"
android:textColor="#000"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />



<TextView
android:id="@+id/year"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/genre"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


</RelativeLayout>

Answer

Try to change your parent row layout's height to wrap_content or fixed dpi.

Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

Source

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

   // your content

</RelativeLayout>