如何创建具有wrap_content宽度且不可滚动的RecyclerView水平?

发布时间:2020-07-06 05:51

我想实现RecyclerView从左到右显示或添加项目,并且当它到达屏幕末尾时,添加的项目将显示在下一行。

enter image description here

不是这样

enter image description here

这是我的代码

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="11dp"
        android:layout_marginEnd="11dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="horizontal"
        tools:itemCount="10" />

我也尝试使用StaggeredLayoutManagerVertical方向(正如我所说,我想从左到右添加项目),但我不想使用SpanGrid一样对齐项目。另外,“列数”不是固定的,可能取决于屏幕大小。

感谢您的帮助,谢谢!

回答1

文档有点误导,但我已经成功使用com.google.android.material.chip.ChipGroup,其中包含ViewSwitcher的列表。作为记录,它还使用了MaterialButton列表。

它确实具有您想要的UI行为,但是与RecyclerView不相关,因此如果您期望使用相同的API,则必须进行一些编码,我建议您扩展自己的MyWrappableView: ChipGroup

回答2

您可以使用FlexboxLayout。

请点击链接。

https://github.com/google/flexbox-layout