如何创建水平 RecyclerView 在屏幕空间已满时自动换行?

Anh*_*hạm 4 tags android android-recyclerview

我正在尝试使用 RecyclerView 在我的应用程序中以编程方式显示标签。它会像这样(抱歉,我现在无法将图像上传到 stackoverflow)

http://anh.im/image/OUA

那么,我如何创建 RecyclerView 从左到右显示我的内容并在 RecyclerView 右边缘自动换行看起来像图像并防止其水平滚动?谢谢你的帮助!

Bho*_*gar 5

使用 实现“com.google.android:flexbox:1.0.0”

只需添加到您的回收视图中

 val decoration = DividerItemDecoration(context, RecyclerView.HORIZONTAL)
    decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_10dp)!!)
    recyclerViewSelectionList.addItemDecoration(decoration)
    val layoutManager = FlexboxLayoutManager(this)
    layoutManager.flexDirection = FlexDirection.ROW
    layoutManager.justifyContent = JustifyContent.FLEX_START
    recyclerViewSelectionList.layoutManager = layoutManager
Run Code Online (Sandbox Code Playgroud)

  • 这是Java代码:- FlexboxLayoutManager layoutManager =new FlexboxLayoutManager(mContext); 布局管理器.setFlexDirection(FlexDirection.ROW); layoutManager.setJustifyContent(JustifyContent.FLEX_START); recyclerViewSelectionList.setLayoutManager(layoutManager); (2认同)