Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

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

Upvotes: 2

Views: 3285

Answers (1)

Bob
Bob

Reputation: 13865

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.

Upvotes: 11

Related Questions