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