Чтобы упростить задачу, у меня есть ConstraintLayout
с двумя дочерними элементами: RecyclerView
и Button
.
. Я хочу, чтобы RecyclerView
начал показываться сверху родительского элемента.Если в RecyclerView
есть только несколько элементов для показа, их следует обернуть.Примерно так:
![enter image description here](https://i.stack.imgur.com/ihZBA.png)
Но, если у RecyclerView
достаточно элементов, чтобы пройти до конца экрана, он должен идти доверхняя часть Button
, а не до нижней части его родителя.Например: ![enter image description here](https://i.stack.imgur.com/fmdsh.png)
Чтобы достичь этого эффекта, я попробовал следующую комбинацию:
<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](https://i.stack.imgur.com/Qmyw7.png)
То есть RecyclerView
располагается в центре между верхней частью родительского элемента и верхней частью кнопки.Это будет нормально, если в RecyclerView
есть много предметов, но нет, если есть только небольшое количество предметов.
Вопрос : Можно ли заставить RecyclerView
вести себя как:
Независимо от количества элементов, установите его верхнее позиционирование так, чтобы оно находилось вверху его родительского элемента.
Если элементов малопросто оберните содержимое до конца последнего элемента.
Если имеется много элементов, разверните до верхней части Button
, а не до нижней части parent
?
PS Решение следует использовать только как родительское ConstraintLayout
.