【问题标题】:LayoutManager that fills all RecyclerView area [duplicate]填充所有 RecyclerView 区域的 LayoutManager [重复]
【发布时间】:2019-01-27 13:10:00
【问题描述】:

我有一个RecyclerView。我想实现以下(每个圆圈是一个RecyclerView 项目):

每个圆圈的大小可能不同。

我尝试使用GridLayoutManager,但事实证明我需要提供spanCount,这不适合我的情况。

【问题讨论】:

标签: android android-layout android-recyclerview


【解决方案1】:

对于具有不同单元格跨度的网格形式的 RecycleView 布局,可以使用GridLayoutManager 来实现。你会在网上找到很多tutorials关于如何实现它。

GridLayoutManager  layoutManager = new GridLayoutManager(this, 6);

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        // Use whatever logic you need here to decide how 
        //  many columns to span for any given row position
        if (position % 2)
            return 3;
        else
            return 6;
    }
});

然后在 onBindViewHolder 中根据需要更新每个视图的 LayoutParams.width。

【讨论】:

  • 我之前也试过这个。这个布局管理器还需要spanCount,这是因为我不知道列数和行数。
  • 获取无法解决方法'setSpanSizeLookup(anonymous android.support.v7.widget.GridLayoutManager.SpanSizeLookup)`错误。
  • 我不认为你可以将setSpanSizeLookup设置为StaggeredGridLayoutManager
  • 那么对于这种情况最好使用 GridLayoutManager,并在绑定时更新视图指标。因此,使用查找来指定一行必须跨越多少列,并使用 onBindViewHolder 更新各个视图指标以相应地呈现所需的列区域。
  • 添加了教程链接。
猜你喜欢
  • 1970-01-01
  • 2021-05-07
  • 2018-07-18
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多