Set span for items in GridLayoutManager using SpanSizeLookup
Asked Answered
G

3

105

I want to implement grid-like layout with section headers. Think of https://github.com/TonicArtos/StickyGridHeaders

What I do now:

mRecyclerView = (RecyclerView) view.findViewById(R.id.grid);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_HEADER:
                        return 1;
                    case MyAdapter.TYPE_ITEM:
                        return 2;
                    default:
                        return -1;
                }
            }
        });

mRecyclerView.setLayoutManager(mLayoutManager);

Now both regular items and headers have span size of 1. How do I solve this?

Garderobe answered 11/11, 2014 at 16:12 Comment(3)
this implementation looks correct to me. Did you debug if your mAdapter.getItemViewType(position) is returning the correct value ?Knighthood
"1" seems like a safer default value than "-1".Clingy
I am a newbie. For me, this link helped me 3 RecyclerView Infinite Scroll ExamplesTiconderoga
G
175

The problem was that header should have span size of 2, and regular item should have span size of 1. So correct implementations is:

mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_HEADER:
                        return 2;
                    case MyAdapter.TYPE_ITEM:
                        return 1;
                    default:
                        return -1;
                }
            }
        });
Garderobe answered 13/11, 2014 at 11:9 Comment(4)
get span size method determines the amount span width your cell is going to take not the number of col row should have !!Zachary
when spanning the first item, it's messing up the height of the next ones. It works on any other item. Any idea ?Bentham
@RonnyShibley any solution for the issue you stated above ... I am also facing the same issue, first item after the header doesn't shows , others are all shown up as requiredGarniture
This is not woking.Hemiplegia
B
46

Header should have a span equal to the span count of the entire list.

mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
           switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_HEADER:
                        return mLayoutManager.getSpanCount();
                    case MyAdapter.TYPE_ITEM:
                        return 1;
                    default:
                        return -1;
                }
    }
});
Buck answered 5/9, 2016 at 9:23 Comment(1)
How comes no one links to the actual documentation: developer.android.com/reference/kotlin/androidx/recyclerview/… Hence, this answer seems more accurate to me then the currently accepted one. Maybe create a const variable with the span count and use it for the GridLayoutManager constructor and in the getSpanSize method to avoid the getSpanCount call for every header.Gristmill
L
2

Answer to my own question: Override the getSpanSizeLookup() from the Activity after setting the adapter.

Lentamente answered 18/11, 2016 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.