【问题标题】:RecyclerView items with very large empty space at bottom after updating to support library 23.2.1 [duplicate]更新为支持库 23.2.1 后,RecyclerView 项目底部有非常大的空白空间 [重复]
【发布时间】:2016-03-19 01:11:56
【问题描述】:

我已将支持库更新到 23.2.1,之后 recyclerview 项目出现了很大的空白空间。 虽然我使用的是 recyclerview 并且它的父视图高度为 WRAP_CONTENT。 我的应用程序一次从服务器获取 10 个项目数据,并在向上滚动时在底部显示一个进度条,因此当时(在显示进度条时)recyclerview 项目显示有很大的空白空间。

片段和适配器的xml文件代码:

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

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingTop="?attr/actionBarSize"
        android:scrollbars="vertical"
        android:id="@+id/recycler_view"/>

    <include layout="@layout/date_picker_layout"
        android:id="@+id/date"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"/>

    <LinearLayout
        android:layout_width="@dimen/sort_view_width"
        android:layout_height="@dimen/sort_view_height"
        android:visibility="gone"
        android:id="@+id/sort_layout"
        android:background="@drawable/date_picker_selector"
        android:baselineAligned="false"
        android:weightSum="2"
        android:orientation="horizontal"
        android:divider="@drawable/vertical_divider"
        android:layout_gravity="center"
        android:gravity="center"
        android:showDividers="middle"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="@dimen/margin_large"
        android:layout_marginBottom="@dimen/margin_large">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="@dimen/button_size"
            android:layout_weight="1"
            android:padding="@dimen/margin_xxsmall"
            android:id="@+id/low_to_high"
            android:src="@drawable/ic_sort_down"/>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="@dimen/button_size"
            android:layout_weight="1"
            android:padding="@dimen/margin_xxsmall"
            android:id="@+id/high_to_low"
            android:src="@drawable/ic_sort_up"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableBottom="@drawable/three_dot"
        android:visibility="gone"
        android:textSize="@dimen/textsize_medium"
        android:text="@string/coming_soon"
        android:id="@+id/no_items"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

适配器.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/itemview">

    <ImageView
        android:id="@+id/image"
        android:layout_height="@dimen/list_image_height"
        android:layout_width="match_parent"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"/>

    <com.example.views.CircularProgressWheel
        android:layout_width="@dimen/progressbar_width"
        android:layout_height="@dimen/progressbar_height"
        android:id="@+id/progress_bar"
        android:visibility="gone"
        wheel:matProg_barColor="@color/primary"
        wheel:matProg_progressIndeterminate="true"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient"
        android:layout_alignBottom="@id/image"
        android:id="@+id/relativeLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:id="@+id/title"
            android:textColor="@color/white"
            android:textSize="@dimen/textsize_medium"
            android:layout_toLeftOf="@+id/price"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="@dimen/margin_xsmall"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_xsmall"
            android:text="@string/location"
            android:id="@+id/location"
            android:textColor="@color/white"
            android:textSize="@dimen/textsize_medium"
            android:layout_below="@+id/title"
            android:layout_toLeftOf="@+id/price"
            android:layout_alignLeft="@+id/title"
            android:layout_alignStart="@+id/title" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/price"
            android:layout_marginRight="@dimen/margin_xsmall"
            android:id="@+id/price_actual"
            android:textColor="@color/white"
            android:textSize="@dimen/textsize_medium"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/price"
            android:id="@+id/price"
            android:textColor="@color/white"
            android:textSize="@dimen/textsize_medium"
            android:layout_below="@+id/title"
            android:layout_alignRight="@+id/price_actual"
            android:layout_alignEnd="@+id/price_actual" />

    </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/margin_xxsmall"
        android:textSize="@dimen/textsize_medium"
        android:background="@drawable/cancel_button_selector"
        android:visibility="gone"
        android:textColor="@color/white"
        android:text="@string/sold_out"
        android:id="@+id/sold_out"
        android:layout_marginRight="@dimen/margin_xsmall"
        android:layout_above="@+id/relativeLayout2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    确保您的回收站视图高度设置为wrap_content23.2 为 RecyclerView 引入了包装内容功能,如果您希望 RecyclerView 像以前一样工作,您应该将高度设置为 wrap_content。

    引用Android Developers Blog

    由于此更改,请务必仔细检查布局参数 您的项目视图:以前忽略的布局参数(例如 MATCH_PARENT 在滚动方向)现在将被完全遵守。

    【讨论】:

    • 获得相同的行为,我将其设置为 match_parent 或 wrap_content
    • 你能贴一些代码吗?您的适配器项 xml 和 recyclerview xml 将是理想的。
    • 不确定你是否已经解决了这个问题。我正在使用支持库版本 23.4.0 并遵循上述建议,但仍然面临与您相同的问题。将 wrap_content 用于项目视图,将正确显示项目,但滚动后,项目之间会有很大的差距。在我的情况下,我尝试将 RecylerView(不是项目视图)设置回 match_parent。总而言之,在我的情况下: - RecyclerView 是 match_parent - 项目视图是 wrap_content 可能适用于您的情况。
    • 我的进度条的布局宽度是 MATCH_PARENT 这就是为什么它出现在全屏上,将其设置为 WRAP_CONTENT 后我的问题就解决了。
    • 在我的例子中,在方法 onCreateViewHolder 上链接到我的 Adapter 类的 xml 有一个带有 android:layout_heigh="match_parent" 的 RelativeLayout,我将其更改为 "wrap_content",它运行良好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多