Aniket Shinde
Aniket Shinde

Reputation: 350

Recyclerview onBindViewHolder called for all items when inside LinearLayout with weights

I recently updated from support library recyclerview-v7:23.1.1 to recyclerview-v7:25.1.0.

My Layout contains 2 recylerviews splitted 50% on the screen. The xml code is as follows:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none"/>

    <android.support.v4.widget.Space
        android:layout_width="@dimen/two_dp"
        android:layout_height="match_parent"
        android:background="@color/dark_gray"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none"/>

</LinearLayout>

Now onBindViewHolder is called for all list items instead of only the visible items. This started happening after update to support library 25.1.0.

It is working fine if weights are removed, but having 2 recylcerviews side by side is required.

How do I tell recyclerview to recycle views instead of loading all?

UPDATE: It works fine on Marshmallow and above devices. The issue is present in Lollipop or below. You can find a demo project here: https://bitbucket.org/aniketit/recyclerviewtest

Upvotes: 19

Views: 4739

Answers (3)

Alessandro Verona
Alessandro Verona

Reputation: 1257

With libraries > than 23.1.1 you can remove

android:layout_width="0dp"

remove it and all works perfectly.

Upvotes: 2

zarvedan
zarvedan

Reputation: 46

I had the same issue. RecyclerView was recycling well in Marshmallow but not before.

My mistake was to put my RecyclerView into a ScrollView. You should check if you have a scrollview, if yes, remove it and your issue will be solved for pre marshmallow devices.

Upvotes: 3

Mike Jancola
Mike Jancola

Reputation: 461

I ran into the same issue which persisted after removing weighted and 0dp elements. In my case, the issue was rather trivial - I had inadvertently put my RecyclerView inside of a NestedScrollView with fillViewPort=true. This causes the Adapter will build all the elements for the view and you will notice a significant delay in responsiveness.

Turns out the issue will occur if you just have the RecyclerView in a plain old ScrollView too.

Upvotes: 13

Related Questions