本文介绍了RecycleView的跨度大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现与上图类似的布局与RecyclerView和GridLayoutManager一起使用时,我尝试根据位置设置setSpanSizeLookup,但无法模仿上面的设计.

I'm trying to achieve a layout similar to the picture abovewith the RecyclerView in conjunction with the GridLayoutManager, i tried setting the setSpanSizeLookup based on position but couldn't mimic the design above..

有人可以帮忙吗?

    mRecyclerView = (RecyclerView) contentView;
    mRecyclerView.setHasFixedSize(false);

    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return 3 - (position % 3);
        }
    });

    mRecyclerView.setLayoutManager(gridLayoutManager);

..

我得到的是什么

What i'm getting

推荐答案

好,您希望将交替的行"平均分配给两个单元格.均匀分割需要偶数列,而3不是偶数.因此,将其更改为2.

Well, you want alternate "rows" to be evenly split between two cells. Evenly split requires an even number of columns, and 3 is not an even number. So, change that to 2.

现在,您希望getSpanSize()返回(按行):

Now, you want getSpanSize() to return (by row):

  • 位置0 = 2(即跨越两个单元格)
  • 位置1和2 = 1(即每个位置占用一个单元格)
  • 位置3 = 2
  • 位置4和5 = 1

(position % 3) > 0 ? 1 : 2似乎可以为您提供.

这篇关于RecycleView的跨度大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:10