【问题标题】:LayoutManager for RecyclerView to set the number of columns automatically according to width of itemsRecyclerView的LayoutManager根据item的宽度自动设置列数
【发布时间】:2016-02-06 11:11:50
【问题描述】:

我正在尝试构建一个涉及 RecyclerView 或 GridView 的 UI,我想根据我动态填充的项目的数量和宽度自动设置列数。

这是我迄今为止所取得的成就的形象......

这是我想要达到的目标的图像......

我的项目布局 xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:background="@drawable/hollow_white_background"
        android:id="@+id/foodItemName"
        android:text="PIZZA"
        android:gravity="center"
        android:textColor="@color/colorGray300"
        android:textStyle="bold"
        android:textSize="15sp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

请帮忙!

【问题讨论】:

  • 请提供您用于配置GridLayoutManager的项目布局xml和代码。
  • 查看您的目标 UI 我认为您无法通过使用带有标准布局管理器的 RecyclerView 来获得它。你需要某种 FlowLayout。有许多可用的实现。例如here,它是我的一个存储库,包括类似的东西。
  • 也许 FlowLayout 是您正在寻找的。请参阅github.com/ApmeM/android-flowlayoutgithub.com/blazsolar/FlowLayout
  • 显然这里是一个可以由 RecyclerView.Adapter 填充的 FlowLayout android-arsenal.com/details/1/…

标签: android android-layout


【解决方案1】:

在您的 xml 文件中,为您的 GridView 添加 android:numColumns="auto_fit" 和 android:stretchmode="columnWidth" 属性。第一个属性根据屏幕大小决定应该有多少列,第二个属性决定如何处理剩余的空间,在这种情况下会增加列宽。

【讨论】:

  • 已经这样做了,但它仍然只显示两列,我的项目向左对齐,不处理额外的空间。我想要的是当我的项目的宽度很小时,它应该显示为 3 列或更多,当宽度很大时,它应该自动显示 1 或 2 列!
  • @AyushMaurya 好吧,这些属性应该完全符合您的需要。你能用你当前的 xml 文件更新问题吗?
  • 这是我使用的xml ,问题还是一样。
  • 我认为出现问题是因为您正在设置特定的 Button(或 TextView,无论是什么)宽度。尝试使每个按钮的宽度为“wrap_content”。
  • 不,它只是“wrap_content”。
【解决方案2】:

尝试使用 StaggeredGridLayoutManager 作为 RecyclerView 的 LayoutManager 并固定 widthitem_layout.xml

【讨论】:

  • StaggeredGridLayoutManager 影响行,而不是列!
猜你喜欢
  • 2013-06-29
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 2014-03-06
  • 1970-01-01
相关资源
最近更新 更多