【问题标题】:Last Item in recyclerview is cut offrecyclerview 中的最后一项被切断
【发布时间】:2024-05-29 16:10:02
【问题描述】:

我正在使用 recyclerview 来显示项目列表,并且约束布局是父视图。布局如下图所示:

  <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <!-- Load the toolbar here -->
        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="?android:attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:background="@color/colorPrimary"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:visibility="visible"
            app:layout_constraintTop_toBottomOf="@+id/toolbar" />

        <ImageView
            android:id="@+id/imageViewContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="parent"
            android:visibility="gone"
            android:src="@drawable/empty_category"/>

        <TextView
            android:id="@+id/textViewContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="Empty Category"
            android:textStyle="bold"
            android:textColor="@color/colorPrimary"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:visibility="gone"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageViewContent" />

    </android.support.constraint.ConstraintLayout>

The adapter layout for each row is presented below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/framelayoutImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/buttonCart"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <ImageView
            android:id="@+id/imageViewCoverArt"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="center" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#59000000"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textViewPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|left"
                android:layout_marginLeft="10dp"
                android:clickable="false"
                android:text="$220"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="@android:color/white"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textViewDetails"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|left"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="8dp"
                android:ellipsize="end"
                android:maxLength="17"
                android:maxLines="1"
                android:text="Lorem ipsum dolor sit amet, consecte"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="@android:color/white" />

        </LinearLayout>

    </FrameLayout>

    <Button
        android:id="@+id/buttonCart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/buttonshape"
        android:text="Add to Cart"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:textColor="@android:color/white"
        android:textStyle="bold"
     />
</LinearLayout>

运行我的应用程序后,布局如下图所示:

我尝试在回收视图中添加底部边距,但这并没有解决问题。我使用以下链接作为参考:RecyclerView is cutting off the last itemRecyclerView cutting off last item 并尝试在没有运气的情况下进行这些更改

【问题讨论】:

标签: android xml android-layout recyclerview-layout


【解决方案1】:

您的RecyclerView 没有受到适当的约束。您可以使用0dp (MATCH_CONSTRAINT) 作为高度并使用所有可用空间:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar" />

或者,如果您想将其保留为 wrap_content,则需要设置 app:layout_constrainedHeight="true" 属性以强制执行约束:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:visibility="visible"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar" />

【讨论】:

  • 我实际上不得不使用app:layout_constrainedHeight="true",因为这里有问题的属性是recyclerview的高度,而不是宽度。
  • 我在 DialogFragment 中有一个 RecyclerView,app:layout_constrainedHeight="true" 修复了它
  • 这就像一个魅力。将 recyclerView Height 更改为 0dp 没有为我显示任何项目。谢谢@Pawel
【解决方案2】:

我尝试了大多数可能站点的所有可用选项,但我没有得到解决方案。 那么,我想我可以使用底部填充吗?是的,这对我有用。

我将代码分享给你。 除了高度、宽度和填充之外,不需要其他属性。

<android.support.v7.widget.RecyclerView
    android:id="@+id/your id name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="?attr/actionBarSize"
    app:layout_constraintTop_toBottomOf="@+id/your field" />

【讨论】:

    【解决方案3】:

    1)Recyclerview的View必须适当约束。 (通过 ConstraintLayout 或任何其他布局参数)。

    2)最后一项占据了RecyclerView内部的空间,因此使用Padding。 (例如 android:paddingBottom="50dp")

    3)在 XML 中使用 android:clipToPadding="false"

    【讨论】:

      【解决方案4】:

      在我的情况下,我的ConstraintLayoutlayout_heightwrap_content,这就是为什么最后一个项目是从底部切割的。所以要解决这个问题:ConstraintLayoutlayout_height 应该是固定的或match_parent

      【讨论】:

        【解决方案5】:

        尝试使用RelativeLayout 而不是ConstraintLayout

        它对我有用,所以特别是 ConstraintLayout 似乎有问题。

        【讨论】: