Grid Recyclerview,偶数行有 3 列,奇数行有 4 列

Grid Recyclerview with 3 columns in even number row and 4 columns in odd number row

如何创建偶数行 3 列和奇数行 4 列的 Grid Recyclerview?

    lLayout = new GridLayoutManager(getActivity(), 4, LinearLayoutManager.VERTICAL, false); // MAX NUMBER OF SPACES

    lLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return (position % 3 == 0 ? 3 : 4);
        }
    });

    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(lLayout);

    if (arrayList != null) {
        adapter = new RecyclerViewAdapter(getActivity(), arrayList);
        recyclerView.setAdapter(adapter);
    }

尝试这样的事情

 layoutManager = new GridLayoutManager(getActivity(), 4);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (position % 3 == 0)
                return 3; 
            else
                return 4; 
        }
    });
    recyclerView.setLayoutManager(layoutManager);

希望对您有所帮助

试试这个,

// Create a grid layout with 12 columns
    // (least common multiple of 3 and 4)
 GridLayoutManager manager = new GridLayoutManager(this, 12, GridLayoutManager.VERTICAL, false);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                // 7 is the sum of items in one repeated section
                switch (position % 7) {
                    // first three items span 3 columns each
                    case 0:
                    case 1:
                    case 2:
                        return 4;
                    // next four items span 2 columns each

                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        return 3;
                }
                throw new IllegalStateException("internal error");
            }
        });
        recyclerView.setLayoutManager(manager);