Android Sticky-footer:将页脚视图与表格对齐,直到达到屏幕大小,然后固定在底部

hta*_*oya 4 android sticky-footer android-recyclerview android-constraintlayout

这应该类似于 iOS 表格视图页脚,也可以在各种网站中看到(粘性页脚)。

我想实现以下目标:

图表

ARecyclerView行数可变的。

A小于屏幕(或父级)尺寸时,B(页脚)应放置在最后一行下方。

A + B大于屏幕尺寸时,B固定在底部,A内容可滚动。

我们目前正在使用onMeasure计算所有组件高度的函数来执行此操作,以便相应地调整 A 的大小。

我想知道是否有更简单的方法可以做到这一点,也许是使用ConstraintLayout.

Paw*_*ski 5

A和放入B垂直堆积链中,并倾斜 使其0与顶部对齐。您还需要进行设置,app:layout_constrainedHeight="true"以便RecyclerView当它变得太大而无法容纳它们时考虑它的约束(match_parent在这种情况下,父母的高度仍然存在):

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/A"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/B" />

    <TextView
        android:id="@+id/B"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Footer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/A"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

上述解决方案似乎不适用于Constraintlayout:2.0.0-beta2,看起来像是该版本中引入的错误。适用于2.0.0-beta11.1.3

另一种方法是将父级的高度设置为wrap_content,然后您可以使用默认的 chainstyle 并消除偏差:

<?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="wrap_content">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/A"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/B" />

    <TextView
        android:id="@+id/B"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Footer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/A"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

该解决方案适用于所有版本。