限制 RecyclerView 的最大尺寸

发布于 2025-01-21 01:56:11 字数 1760 浏览 0 评论 0原文

我将一个屏幕放在一起,其中3个控件垂直堆叠。具体而言,夹在两个TextView之间的回收模型。我有两种情况要解决。

  1. 当循环系统中显示很少的项目时,应将3个项目包装在屏幕顶部,下面有空白。
  2. 当循环系统中显示许多项目时,全屏幕将与屏幕底部的最后一个文本视图一起使用,并在中心循环中滚动。

我努力将上一个文本视图的位置正确正确,因为如果回收库中有很多项目,则可以将其从屏幕上推开,或者埋在回收界后面。我需要的一种方法是告诉回收即包装内容,但是在开始将文本视图从屏幕上推开时停止增长。

以下是我的布局,有什么建议吗?

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

I'm putting together a screen with 3 controls stacked vertically. Specifically, a RecyclerView sandwiched between two TextView. I have two situations I am trying to address.

  1. When few items are displayed in the RecyclerView the 3 items should be packed towards the top of the screen with blank space BELOW them.
  2. When many items are displayed in the RecyclerView the full screen will be used with the last TextView along the bottom of the screen and scrolling in the center RecyclerView.

I'm struggling to get the position of the last TextView correct as it either gets pushed off the screen or buried behind the RecyclerView if there are many items in the RecyclerView. What I need is a way to tell the RecyclerView to wrap contents, but stop growing when it starts to push the TextView off of the screen.

Below is my layout, any suggestions?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2025-01-28 01:56:11

请尝试以下操作:

<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,以将链始终置于顶部;
  2. 向 RecyclerView 添加了 app:layout_constrainedHeight="true" 以确保视图不会离开屏幕;
  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>

Try the following:

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

With three items in the RecyclerView:

enter image description here

With 50 items in the RecyclerView:

enter image description here

The key changes are:

  1. Added app:layout_constraintVertical_bias="0.0" to the top TextView to position the chain always at the top;
  2. Added app:layout_constrainedHeight="true" to the RecyclerView to make sure views don't go off-screen;
  3. Changed match_parent to 0dp and set the horizontal constraints for direct children of the ConstraintLayout.
  4. Changed the RecyclerView's height to '0dp'.

Update: Although the above works, it is a little overdone. As noted in the comments, the same effect can be accomplished by simply setting the height of the RecyclerView to 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文