AndroidX中的Recycler View如何使用match constraint(0dp)和wrap content行为?

7
我这里有一个简单的recycler view,我想要的是:
当列表很短时:将按钮固定在recycler view下方
当列表很长时:将按钮固定在屏幕底部,同时recycler view能够正确包裹并滚动到底部
<?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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintBottom_toTopOf="@id/btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_bias="0.0"
        tools:itemCount="50"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:text="example"
        android:background="#00ffff"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@id/rv_user_address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

什么时候使用wrap_content

<androidx.recyclerview.widget.RecyclerView
android:layout_height="wrap_content"
...

当列表较短时,短列表可以正确地将按钮粘在下面,但当列表较长时,按钮会偏离屏幕。

当约束条件为:0dp 时:

<androidx.recyclerview.widget.RecyclerView
android:layout_height="0dp"
...

长列表的行为是正确的,但短列表下面的按钮没有固定在列表下方。
我想不出解决办法。谢谢您的帮助。

enter image description hereenter image description hereenter image description hereenter image description here


你尝试过使用 constrainedHeight 吗?请记住,如果这样做,您需要在 RecyclerView 上使用 wrap - Martin Marconcini
3个回答

13

只需添加这一行代码:

app:layout_constrainedHeight="true"

将以下内容添加到您的RecyclerView:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user_address"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="20dp"
        app:layout_constrainedHeight="true"   <--  Add this line
        app:layout_constraintBottom_toTopOf="@id/btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_bias="0.0"
        tools:itemCount="50"/>

这是你应该尝试的。如果你需要更多关于它的信息,你可以查看我的另一个问题的答案,它有一个类似的问题但是水平方向。(不是完全相同的条件,但是有一个小部件应该在另一个小部件旁边直到达到边界/边缘的相同概念)。 - Martin Marconcini

1

试试这个。首先将按钮对齐到布局的末尾,然后在我们的 RecyclerView 中添加 app:layout_constrainedHeight="true" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toTopOf="@id/btn"。这将使您的 RecyclerView 覆盖除位于布局底部的按钮之外的所有布局。如果您还有任何问题,请随时问我。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

      <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_user_address"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="15dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="10dp"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@id/btn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintVertical_chainStyle="packed"
    tools:itemCount="100" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:background="#00ffff"
        android:gravity="center"
        android:orientation="horizontal"
        android:text="example"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here


0
将您的约束布局更改为线性布局。 在线性布局中添加权重总和。 为您的可回收视图和按钮分配所需的权重。
    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10">

   <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user_address"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9" 
        ...
       />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ...
        />
</LinearLayout>

请确保所有权重的总和不超过您的权重总和。 在当前情况下,回收站视图将占父级的90%,而10%将由按钮使用。随意调整这些数字以实现所需的结果。


请不要使用LinearLayout,它是不必要的,如果不小心使用“权重”可能会对性能产生重大影响。 - Martin Marconcini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接