【问题标题】:Recycler View GridLayoutManager - Weights for each rowRecycler View GridLayoutManager - 每行的权重
【发布时间】:2017-01-18 15:41:00
【问题描述】:

我正在尝试使用完成以下行为的 GridLayoutManager 实现 RecyclerView:

  -------------
  |           |
  |           |
  -------------
  |       |   |
  |       |   |
  -------------
  |   |       |
  |   |       |
  -------------
  |       |   |
  |       |   |
  -------------
  |   |       |
  |   |       |
  -------------

想法是第一项总是占据整行,然后接下来的行被分成3个空间,有2个项目,一个项目的权重为2,另一个项目的权重为1(类似于LinearLayout )。

我怎样才能完成这样的行为?

【问题讨论】:

  • 通过实现自己的RecyclerView.LayoutManager

标签: java android android-layout android-recyclerview


【解决方案1】:

我想通了,正如 Selvin 建议的那样,我查找了自定义 LayoutManager,发现重写方法 getSpanSize() 对我有用。

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            Category item = mCategories.get(position);
            return item.weight;
        }
    });

【讨论】:

  • 但这只会改变项目占用的跨度数。它不会改变跨度重量本身。例如,如果总跨度计数为 4 并且此方法返回 3,则该项目将占据行的 75%,但跨度仍然相等。有没有办法改变跨度本身的重量?那就是跨度数是 2 但跨度权重不同?
猜你喜欢
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多