繁体   English   中英

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

[英]Android sticky-footer: Align footer view to table, until it reaches screen size, then become fixed.at the bottom

这应该类似于 iOS 的 tableview 页脚,也可以在各种网站上看到 (sticky-footer)。

我想实现以下目标:

图表

A是具有可变行数的RecyclerView

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

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

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

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

AB放在一个偏置为0的垂直填充链中,使其与顶部对齐。 您还需要为RecyclerView设置app:layout_constrainedHeight="true" ,以便在它变得太大而无法容纳它们时考虑它的约束(在这种情况下,父级的高度仍然是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>

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

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

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

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

试试 ConstraintLayout,这应该是可能的,只需固定 Bs 高度并将其约束到底部和 A 并将偏差调到顶部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM