android - ConstraintLayout 内的 RecyclerView - 起始位置在父级顶部

标签 android android-layout android-recyclerview android-constraintlayout recyclerview-layout

为了简单起见,我有一个 ConstraintLayout 和 2 个 child :一个 RecyclerView 和一个 Button

我希望 RecyclerView 从父级的顶部开始显示。 如果在 RecyclerView 中只有几个项目要显示,则应该将它们包装起来。像这样:

enter image description here

但是,如果 RecyclerView 有足够的项目可以显示到屏幕末尾,它应该显示到 Button 的顶部,而不是显示到 Button 的底部它的 parent 。 像这样: enter image description here

为了达到这个效果,我尝试了以下组合:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/my_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constrainedHeight="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

RecyclerView 只显示几个项目时,这个解决方案非常好。否则,如果它必须扩展到屏幕之外,它将位于 Button 后面。

将上述 xml 修改为(注意 RecyclerView 处的 app:layout_constraintBottom_toTopOf="@id/my_button" 行):

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/my_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/my_button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我得到的结果是:

enter image description here

也就是说,RecyclerView 位于父级顶部和按钮顶部之间的中心。 如果 RecyclerView 有很多项,这是可以的,但如果只有少量项,则不行。

问题: 是否可以让我的 RecyclerView 表现得像:

  1. 无论项目的数量如何,使其顶部定位在其父项的顶部。

  2. 如果项目较少,则将内容包裹到最后一项的底部。

  3. 如果项目很多,扩展到Button的顶部,而不是parent

    的底部

?

附言应考虑仅将解决方案用作父 ConstraintLayout

最佳答案

您可以使用值为 0app:layout_constraintHorizo​​ntal_bias 属性将您的 RecyclerView 与其顶部约束对齐(1 将与底部约束对齐)。需要设置顶部和底部约束才能使用此偏差。另一方面,如果未设置底部约束,则无法阻止 RecyclerView 与按钮重叠。这就是为什么设置底部约束并保持 app:layout_constrainedHeight="true" 很重要,以确保在其高度设置为 时强制执行 View 的 约束>wrap_content.

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constrainedHeight="true"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintBottom_toTopOf="@id/my_button"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/my_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于android - ConstraintLayout 内的 RecyclerView - 起始位置在父级顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56741152/

相关文章:

android - Xamarin,安卓。每次我在设备上运行该应用程序时,该应用程序都会被卸载然后重新安装。

java - Android Studio 应用程序可在模拟器中运行,但不能在真实设备上运行

android - 布局编辑器可以从 Eclipse 中提取并独立运行吗?

java - 如何在更改父项后强制 View 重新计算其位置

java - 如何检测用户何时完成向 RecyclerView 中的 EditText 输入值?

android - 删除所选单元格的拖放

android - "CREATE TABLE"附近的 SQLite 语法错误

android - fragment 中的 getMapAsync()

java - 从 Android 中的回收器适配器调用 Activity 方法的正确方法?

java - 按下后退时将进度条重置为 0