在不同的行RecyclerView LayoutManager的不同跨度数 [英] RecyclerView LayoutManager different span counts on different rows

查看:267
本文介绍了在不同的行RecyclerView LayoutManager的不同跨度数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个 RecyclerView.LayoutManager ,让我对不同行的重复图案指定不同的跨度数。例如2,3与10个项目是这样的:

I want a RecyclerView.LayoutManager that allows me to specify different span counts for different rows as a repeating pattern. For example 2,3 with 10 items would look like this:

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

我可以想办法与 GridLayoutManager SpanSizeLookup 来破解这一点,但已经有人想出了一个更清洁办法做到这一点?

I can think of a way to hack this with GridLayoutManager and SpanSizeLookup but has anybody come up with a cleaner way to do this?

推荐答案

要做到你想要什么,你可能有写自己的的LayoutManager

To do what you want, you probably have to write your own LayoutManager.

我觉得这是比较容易:

    // Create a grid layout with 6 columns
    // (least common multiple of 2 and 3)
    GridLayoutManager layoutManager = new GridLayoutManager(this, 6);

    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // 5 is the sum of items in one repeated section
            switch (position % 5) {
            // first two items span 3 columns each
            case 0:
            case 1:
                return 3;
            // next 3 items span 2 columns each
            case 2:
            case 3:
            case 4:
                return 2;
            }
            throw new IllegalStateException("internal error");
        }
    });

这篇关于在不同的行RecyclerView LayoutManager的不同跨度数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆