【问题标题】:How to set number of columns based on text length in recyclerview如何在recyclerview中根据文本长度设置列数
【发布时间】:2018-10-30 03:39:32
【问题描述】:

我正在使用 recyclerview 和 GridLayoutManager 在 textView 中显示文本。我想显示这些项目,如图 1 所示。 应用程序的要求是在此处输入图像描述如果 textview 需要 2 行来显示设置的列数为 1,否则将其设为 2。 我尝试使用下面链接中的 GridAutoFitLayoutManager.java,但它没有用。 https://gist.github.com/omidraha/af3aa017d4ec06342bdc03c49d4b83b1

到目前为止,我已将列数设置为 2。下面是我的代码 hotAdapter=新的 HotAdapter(this,hotItems,this); hotRecyclerView.setAdapter(hotAdapter);

    GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);

    hotRecyclerView.setLayoutManager(gridLayoutManager);

【问题讨论】:

标签: android android-recyclerview gridlayoutmanager


【解决方案1】:

你可以试试flexbox layout。将 flexbox 布局添加到项目后,您可以 设置它的 direction=row 和 wrap=wrap。

<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexDirection="row"
app:flexWrap="wrap" >
    <TextView />
    <TextView />
    ...
</com.google.android.flexbox.FlexboxLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多