RecyclerView 在 23.2.0 之后有大空 space 的项目

RecyclerView items with big empty space after 23.2.0

在我的 RecyclerView 中更新到 v23.2.0 后,我的项目之间有巨大的空垂直 space。

我的项目布局非常简单:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"                                    
       android:orientation="vertical" >

根据 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.

由于此更改,请务必仔细检查项目视图的布局参数之前忽略的布局参数(例如MATCH_PARENT 在滚动方向)现在将得到充分尊重

在您的项目布局中,您必须更改:

android:layout_height="match_parent"

android:layout_height="wrap_content" 

您必须使 Recyclerview 和项目布局高度包裹内容。

这是我的工作。

<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>

就我而言,我已经进行了新的 wrap_content 更改,但在 onMove 或 onSwipe 期间仍然存在差距。我通过添加以下代码解决了这个问题:

mRecyclerView.getLayoutManager().setMeasurementCacheEnabled(false);

None 的给定解决方案有效。

设置layout_height = "wrap_content"也不行。

我的列表设计的父级布局是 ConstraintLayout,这就是问题所在(不知道是什么)。但是将父布局更改为 RelativeLayout 后一切正常。