簡體   English   中英

限制 RecyclerView 的最大尺寸

[英]Constrain max size of RecyclerView

我正在將一個屏幕與 3 個垂直堆疊的控件放在一起。 具體來說,一個 RecyclerView 夾在兩個 TextView 之間。我有兩種情況要解決。

  1. 當 RecyclerView 中顯示的項目很少時,這 3 個項目應該被打包到屏幕頂部,並在它們下方留有空白。
  2. 當 RecyclerView 中顯示很多項目時,將使用全屏,最后一個 TextView 位於屏幕底部,並在中央 RecyclerView 中滾動。

我正在努力使最后一個 TextView 中的 position 正確,因為如果 RecyclerView 中有很多項目,它要么被推出屏幕,要么被埋在 RecyclerView 后面。 我需要的是一種方法來告訴 RecyclerView 包裝內容,但在它開始將 TextView 推出屏幕時停止增長。

下面是我的布局,有什么建議嗎?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_height="match_parent">

    <TextView
        android:id="@+id/topItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/middleItem"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/middleItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@+id/bottomItem"
        app:layout_constraintTop_toBottomOf="@+id/topItem" />

    <TextView
        android:id="@+id/bottomItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|top"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/middleItem" />

</androidx.constraintlayout.widget.ConstraintLayout>

嘗試以下操作:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/topItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the top item."
        app:layout_constraintBottom_toTopOf="@+id/middleItem"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/middleItem"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="none"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomItem"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topItem"
        tools:itemCount="3" />

    <TextView
        android:id="@+id/bottomItem"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|top"
        android:text="This is the bottom item."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/middleItem" />

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView中包含三個項目:

在此處輸入圖像描述

RecyclerView中有 50 個項目:

在此處輸入圖像描述

主要變化是:

  1. 添加app:layout_constraintVertical_bias="0.0"到頂部TextView到 position 鏈總是在頂部;
  2. RecyclerView添加了app:layout_constrainedHeight="true"以確保視圖不會離開屏幕 go;
  3. match_parent更改為0dp並為 ConstraintLayout 的直接子級設置水平約束
  4. RecyclerView 的高度更改為“0dp”。

更新:雖然上面的方法有效,但有點過頭了。 如評論中所述,只需將RecyclerView的高度設置為0dp即可實現相同的效果。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/topItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the top item."
        app:layout_constraintBottom_toTopOf="@+id/middleItem"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/middleItem"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@+id/bottomItem"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topItem"
        tools:itemCount="3" />

    <TextView
        android:id="@+id/bottomItem"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|top"
        android:text="This is the bottom item."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/middleItem" />

</androidx.constraintlayout.widget.ConstraintLayout>

暫無
暫無

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

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