2016-03-01 75 views
33

更新到v23.2.0在我的RecyclerView我有项目之间有巨大的空的垂直空间,项目之间。RecyclerView物品后23.2.0空间很大

我的项目布局很简单:

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

此错误仍存在于23.4.0中,其中我有nestedScrollView {recyclerview。 items()}当我删除这些项目中的少数意味着排序会留下空的空间悲伤.... –

+0

这不是一个错误,现在的预期行为。相关的问题[这里](http://stackoverflow.com/q/35677694/1009132),[这里](http://stackoverflow.com/q/35747268/1009132)和[这里](http:// stackoverflow。 com/q/37504796/1009132) – albodelu

回答

106

按照doc

与发行23.2.0有一个令人兴奋的新功能的布局管理API:自动测量
这允许RecyclerView根据其内容的大小自行调整大小。这意味着以前不可用的场景,例如使用WRAP_CONTENT作为RecyclerView的维度,现在可以实现。
您会发现所有内置的LayoutManagers现在都支持自动测量。

由于这一变化,一定要仔细检查布局参数,您的项目意见以前被忽视的布局参数(如MATCH_PARENT在滚动方向)将现在得到充分尊重

在您的项目布局,你必须改变:

android:layout_height="match_parent" 

android:layout_height="wrap_content" 
+2

太棒了!我不知道它是什么... –

+0

@GabrielMorenoIbarra感谢你拯救了我的生命 –

+2

谢谢我的朋友,因为意外和反直觉的行为,挽救了我一个沮丧的夜晚。布拉沃。 –

13

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

3

这是为我工作。

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

2

在我的情况,我提出了新的变化WRAP_CONTENT但仍有onMove或onSwipe过程中表现出来的空隙。我通过添加以下代码解决了这个问题:

mRecyclerView.getLayoutManager().setMeasurementCacheEnabled(false);