У меня есть RecyclerView, который я пытаюсь заполнить объектами CardView. Корневое представление для каждого - ConstraintLayout.
Я хочу, чтобы каждая карта в моем Recyclerview занимала 25% вертикального пространства экрана и была сложена. Я использовал различные рекомендации, чтобы установить эти ограничения.
Я заметил, что, хотя я могу просматривать CardView, между карточками достаточно места. Похоже, что RecyclerView заполняет представления, которые являются высотой match_parent, когда я хочу, чтобы CardView Stacked друг на друга:
RecyclerView:
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.v7.widget.RecyclerView
android:id = "@+id/recycler_view_ingredients"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
>
</android.support.v7.widget.RecyclerView>
Индивидуальный CardView:
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent">
<android.support.constraint.Guideline
android:id = "@+id/guideline_zero"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
app:layout_constraintGuide_percent = ".025" />
<android.support.constraint.Guideline
android:id = "@+id/guideline_one"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
app:layout_constraintGuide_percent = "0.975" />
<android.support.constraint.Guideline
android:id = "@+id/guideline_two"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.025" />
<android.support.constraint.Guideline
android:id = "@+id/guideline_four"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.25" />
<android.support.v7.widget.CardView
android:id = "@+id/recipe_cardview"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintEnd_toStartOf = "@+id/guideline_one"
app:layout_constraintStart_toStartOf = "@+id/guideline_zero"
app:layout_constraintTop_toTopOf = "@+id/guideline_two"
app:layout_constraintBottom_toTopOf = "@+id/guideline_four">
<TextView
android:id = "@+id/id_of_recipe_item"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center" />
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>




Именно ConstraintLayout определяет высоту каждого элемента RecyclerView. Поскольку высота ConstraintLayout равна match_parent, каждый элемент занимает весь экран. В ConstraintLayoutCardView займет 25%. Остальные 75% будут пустыми. Вот почему вы видите то, о чем пишете.
Итак, вам нужен способ сделать ConstraintLayout равным 25% высоты экрана. К сожалению, невозможно указать высоту 25% для дочерних элементов RecyclerView напрямую, кроме как с помощью кода. (Это не совсем так: например, если у вас есть RecyclerVIew, определенный в 100dp, вы можете определить ConstraintLayout как 25dp и покончить с этим. Я предполагаю, что вы не хотите указывать точные высоты в своем коде.)
Чтобы получить высоту 25%, вам необходимо определить высоту RecyclerViewпосле макета и установить высоту ConstraintLayout в onCreateViewHolder() адаптера для RecyclerView.
@ tccpg288 На рекомендации могут ссылаться только братья и сестры руководства. Директива не может выходить за пределы своего ConstraintLayout, чтобы изменить размер макета.
Понятно, я рассматриваю это как своего рода ограничение ConstraintLayout, вы согласны? По сути, у него нет функции wrap_content, и когда я применяю wrap_content к Constraint_Layout для элементов RecyclerView, он сводит к минимуму все мои представления.
Я бы подумал, что есть атрибут ConstraintLayout, но, похоже, это ограничение. Я не могу использовать свои рекомендации для корневого ConstraintLayout?