Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StaggeredGridLayoutManager. How to have one item in first row and 2 item other rows

I am using recyclerview with staggeredGridLayoutManager. What i am trying to do is to show one header item in first row. then i want to show 2 items in each below row. But i am unable to do so.

Here is code that i used.

    recyclerView = (RecyclerView)findViewById(R.id.recycler);
    staggeredGridLayoutManager = new StaggeredGridLayoutManager(1,1);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);

And these are the results

enter image description here

enter image description here

Help me with this. thanks

like image 831
Zeeshan Shabbir Avatar asked Dec 12 '25 08:12

Zeeshan Shabbir


1 Answers

You could use GridLayoutManager.

GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);

manager.setSpanSizeLookup(
    new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
             // 2 column size for first row
             return (position == 0 ? 2 : 1);
        }
    });

Here we are creating a GridLayoutManager with 2 grid columns. Then only for the first row, we are setting the span size to 2.

like image 103
Bob Avatar answered Dec 14 '25 21:12

Bob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!