47

After update to v23.2.0 in my RecyclerView I have items with huge empty vertical space, between the items.

My item layout is very simple:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"                                    
       android:orientation="vertical" >
Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Still this error persist in 23.4.0 where i have nestedScrollView{ recyclerview . items()} when i remove few of those items meaning a sort would leave empty space sadly.... – EngineSense Aug 29 '16 at 03:13
  • It's not an error, the expected behavior now. Related questions [here](http://stackoverflow.com/q/35677694/1009132), [here](http://stackoverflow.com/q/35747268/1009132) and [here](http://stackoverflow.com/q/37504796/1009132) – albodelu May 09 '17 at 01:40

5 Answers5

134

According to the doc

With the release 23.2.0 there is an exciting new feature to the LayoutManager API: auto-measurement!
This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible.
You’ll find all built in LayoutManagers now support auto-measurement.

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.

In your item layout you have to change:

android:layout_height="match_parent"

with

android:layout_height="wrap_content" 
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    My item layout was using a Constraint Layout which was causing some problems as well, switching that to a Linear Layout in accordance with the above answer solved my issue – Charles Woodson Apr 12 '18 at 17:30
  • @GabrielMoreno When I change the `layout_height` to `wrap_content` in my recyclerView and my imageView , images didn't appear – Ahmed Jul 30 '18 at 10:14
15

You must make Recyclerview and item layout heights wrap content.

Göksel Güren
  • 1,479
  • 13
  • 21
  • When I change the `layout_height` to `wrap_content` in my recyclerView and my imageView , images didn't appear – Ahmed Jul 30 '18 at 10:15
6

In my case, I had made the new wrap_content changes but there was still a gap that manifested during onMove or onSwipe. I solved that by adding this code:

mRecyclerView.getLayoutManager().setMeasurementCacheEnabled(false);
Lee Hounshell
  • 842
  • 9
  • 10
3

This is work for me.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:elevation="7dp"
    app:cardCornerRadius="7dp"
    app:cardMaxElevation="7dp"
    app:contentPadding="7dp"
    android:layout_margin="5dp"
    >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_allfeedbacks_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:textSize="14dp"
        android:textStyle="bold"
        />
    <TextView
        android:id="@+id/tv_allfeedback_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        />
    <TextView
        android:id="@+id/tv_allfeedbacks_comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_allfeedbacks_title"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        />
        <TextView
            android:id="@+id/tv_allfeedbacks_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_margin="5dp"
            />
        <RatingBar
            android:id="@+id/ratingbar_allfeedbacks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:numStars="5"
            android:layout_margin="5dp"
            style="?attr/ratingBarStyleSmall"
            />
        </RelativeLayout>

</android.support.v7.widget.CardView>

Basant
  • 741
  • 7
  • 16
1

None of the given Solution worked.

Setting layout_height = "wrap_content" did not work either.

Parent layout of my list design is ConstraintLayout and that was the problem(don't know what). But after changing parent layout to RelativeLayout everything work fine.

Shadman Akhtar
  • 99
  • 2
  • 11