RecyclerView显示在ConstraintLayout2.0.0-beta7上,但显示在1.1.3上



我在ConstraintLayout中有一个RecyclerView。一切都显示出来了,一切正常。

问题是2.0.0-beta7的性能确实很差,所以我决定将其降级到1.1.3的稳定版本。性能非常好,但现在的问题是没有显示recyclerView。

recyclerView有一个wrap_content高度,但在Layout Inspector上检查时,它显示为0。我是不是错过了什么?

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:clipToPadding="false"
android:orientation="horizontal"
android:paddingStart="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar_layout"
tools:listitem="@layout/radio_button" />

在使用ConstraintLayout时,我们往往会错过一些东西。其中一个是在apt时使用0dp,另一个是对几乎所有方向进行约束。

您没有对视图的底部进行任何约束。

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:clipToPadding="false"
android:orientation="horizontal"
android:paddingStart="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar_layout"
app:layout_constraintVertical_bias="0.0"
tools:listitem="@layout/radio_button" />

如果您希望回收器视图位于toolbar_layout和父级底部的中心,则可以删除app:layout_constraintVertical_bias="0.0"。如果你想让你的回收者观点停留在父母的底部,你可以把偏差的值改为"0";1.0〃;。

此外,请确保使用了正确的orientation。如果方向应为horizontal,则可能需要将布局的宽度更改为0dpmatch_parent

最新更新