ConstraintLayout中的RecyclerView-父级顶部的起始位置

时间:2019-06-24 17:12:21

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

为简单起见,我有一个ConstraintLayout,有2个孩子:RecyclerViewButton

我希望RecyclerView从父级的顶部开始显示。 如果RecyclerView中仅显示少量项目,则应将它们包装起来。像这样:

enter image description here

但是,如果RecyclerView有足够的项目可以显示到屏幕末尾,则应改为Button的顶部,而不是其父项的底部。 像这样: 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修改为(注意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>

我得到的结果是:

enter image description here

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

问题: 是否可以使我的RecyclerView像这样:

  1. 不管项目的数量如何,都应使其最高位置位于其父项的顶部。

  2. 如果物品很少,则将内容包裹到最后一个物品的底部。

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

P.S。该解决方案应仅用作父级ConstraintLayout

2 个答案:

答案 0 :(得分:0)

<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="0dp"
        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>

答案 1 :(得分:0)

您可以使用值为app:layout_constraintHorizontal_bias的{​​{1}}属性,将0与其顶部约束对齐(RecyclerView将与底部约束对齐)。为了使用此偏差,需要同时设置顶部和底部约束。另一方面,如果未设置底部约束,那么您将无法防止1与按钮重叠。这就是为什么设置底部约束并保留RecyclerView以确保在其高度设置为app:layout_constrainedHeight="true"时强制执行View's约束的重要性。

wrap_content的顶部约束也不正确,因为其顶部应约束在父对象的顶部而不是底部。

RecyclerView