RecyclerView items with big empty space after 23.2.0
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" >
According to the doc
With the release 23.2.0 there is an exciting new feature to the LayoutManager API: auto-measurement!
This allows aRecyclerView
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"
You must make Recyclerview and item layout heights wrap content.
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);
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>