Massimo Milazzo
Massimo Milazzo

Reputation: 3532

How to add extra space to RecyclerView with clipToPadding=false to prevent premature recycling

I have the following layout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/searchView"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

which is a simple RecyclerView below a SearchView. I want the RecyclerView's views to scroll behind the SearchView, so clipChildren and clipToPadding are set to false. My problem is that RecyclerView is (rightly) recycling its adapter's views when they are still visible, just because they are out of the RecyclerView's area. Is there a way to add an extra space to the RecyclerView within which views are not recycled?

EDIT, a gif showing the problem:

the behaviour

Upvotes: 1

Views: 388

Answers (1)

Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

This method should help you setItemViewCacheSize(int size). It sets the number of offscreen views to retain before adding them to the potentially shared recycled view pool. More about it you can find here.

Upvotes: 1

Related Questions