When RecycleView encounters layout_width="0dp"

When using several RecycleView s to arrange horizontally, the parent layout uses LinearLayout with the following layout:

    <LinearLayout
        android:id="@+id/lottery_area"
        android:layout_width="225dp"
        android:layout_height="110dp"
        android:gravity="center"
        android:orientation="horizontal">

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

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

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

    </LinearLayout>

When the data list added to RecycleViewAdapter after this setting is more or infinite, everything works fine with the 6.0 system, but the mobile phone running with the 5.0 system reports an exception, suggesting that the ViewHolder has a java.lang.OutOfMemory exception.

WHY???

When I encountered this error, I was confused.Why does it cause memory overflow?Didn't RecyvleView just create part of the ViewHolder and then reuse it?

The logs are then printed in the onCreateViewHolder and onBindViewHolder methods of the Adapter, and they are continuously printed as soon as they run, showing that onCreateViewHolder is constantly being called.

11-22 16:49:02.400 22383-22383/com.example.skybdemq.testglsurfaceview I/Adapter: com.example.skybdemq.testglsurfaceview.MainActivity$Adapter@18b3ad39onCreateViewHolder:7580
11-22 16:49:02.400 22383-22383/com.example.skybdemq.testglsurfaceview I/Adapter: com.example.skybdemq.testglsurfaceview.MainActivity$Adapter@18b3ad39onCreateViewHolder:7581
11-22 16:49:02.400 22383-22383/com.example.skybdemq.testglsurfaceview I/Adapter: com.example.skybdemq.testglsurfaceview.MainActivity$Adapter@18b3ad39onCreateViewHolder:7582

WHY??

Although I returned Integer.MAX_VALUE at getItemCount(), I shouldn't have been creating many ViewHolders without displaying them.The initial attempt to set getItemCount to 100 did not cause memory overflow, but onCreateViewHolder was also called more than 100 times.My face was blurred beyond my knowledge.

Okay, there's always a problem to solve.After various investigations and omitting a large slot, my blind cat really hit a dead mouse.Set the layout_width="0dp" of the layout recycleview above to layout_width="match_parent", and it works fine.Unbelievable!!!Another attempt was made to set the layout to vertical arrangement and layout_height="match_parent" to layout_height="0dp", but everything worked, except layout_width="0dp".

So there are various searches to see if you can find posts with the same problems on the Internet, but unfortunately, they are not.Maybe there are very few RecycleViews side by side horizontally.A similar finding in stackoverflow, however, occurs when the 23.2.x version of RecycleView sets layout_height="0dp".Originally a bug in Android.I'm using version 26.0.2.

https://stackoverflow.com/questions/36065736/recyclerview-23-2-x-height-0dp-causes-onbind-called-for-all-items-and-scrolltopo

Keywords: Android Mobile Java

Added by AlexRodr on Sat, 18 May 2019 12:21:05 +0300