Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 728
Aniket Shinde Avatar asked Jan 05 '17 15:01

Aniket Shinde


People also ask

How often is onBindViewHolder called?

However, in RecyclerView the onBindViewHolder gets called every time the ViewHolder is bound and the setOnClickListener will be triggered too. Therefore, setting a click listener in onCreateViewHolder which invokes only when a ViewHolder gets created is preferable.

How many times onCreateViewHolder called?

By default it have 5. you can increase as per your need. Save this answer.

What is the use of onBindViewHolder?

onBindViewHolder. Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.


2 Answers

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.

like image 167
Mike Jancola Avatar answered Sep 17 '22 00:09

Mike Jancola


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.

like image 33
zarvedan Avatar answered Sep 17 '22 00:09

zarvedan