ConstraintLayout 内的 RecyclerView - 父级顶部的开始位置

Dio*_*raj 2 android android-layout android-recyclerview recyclerview-layout android-constraintlayout

为简单ConstraintLayout起见,我有2 个孩子: aRecyclerView和 a Button

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

在此处输入图片说明

但是,如果RecyclerView有足够的项目可以到达屏幕末尾,则它应该到达 的顶部,而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_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

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

修改上述的XML(注意行之后app:layout_constraintBottom_toTopOf="@id/my_button"RecyclerView):

<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>
Run Code Online (Sandbox Code Playgroud)

我得到的结果是:

在此处输入图片说明

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

问题:是否有可能让我的RecyclerView行为像:

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

  2. 如果项目很少,只需将内容包装到最后一个项目的底部。

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

?

PS 该解决方案应考虑ConstraintLayout仅用作父级。

Paw*_*ski 7

您可以使用app:layout_constraintHorizontal_bias值为的属性0将您RecyclerView的顶部约束1对齐(将与底部约束对齐)。需要设置顶部和底部约束以使用此偏差。另一方面,如果未设置底部约束,则您将无法防止RecyclerView按钮与按钮重叠。这就是为什么设置底部约束并保持app:layout_constrainedHeight="true"以确保View's在其高度设置为 时强制执行约束很重要的原因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>
Run Code Online (Sandbox Code Playgroud)