簡體   English   中英

recyclerview 中的最后一項被切斷

[英]Last Item in recyclerview is cut off

我正在使用 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 cut off the last itemRecyclerView cut off last item並嘗試進行這些更改但沒有運氣

您的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" />

我嘗試了大多數可能網站上的所有可用選項,但沒有得到解決方案。 那么,我想我可以使用底部填充嗎? 是的,這對我有用。

我把代碼分享給你。 除了高度、寬度和填充之外,不需要更多屬性。

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

1)Recyclerview的View必須適當約束。 (通過 ConstraintLayout 或任何其他布局參數)。

2)最后一項占用了 RecyclerView內部的空間,因此使用Padding (例如 android:paddingBottom="50dp")

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

好吧,就我而言,我的ConstraintLayoutlayout_heightwrap_content ,這就是為什么最后一項是從底部切割的。 所以要解決這個問題: ConstraintLayoutlayout_height應該是 fixed 或match_parent

嘗試使用RelativeLayout而不是ConstraintLayout

它對我有用,所以它似乎是 ConstraintLayout 的一個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM