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?

  • this implementation looks correct to me. Did you debug if your `mAdapter.getItemViewType(position)` is returning the correct value ? – yigit Nov 12 '14 at 20:14
  • 1
    "1" seems like a safer default value than "-1". – BladeCoder Dec 30 '16 at 13:44
  • 1
    I am a newbie. For me, this link helped me [3 RecyclerView Infinite Scroll Examples](https://androidride.com/android-recyclerview-load-more-on-scroll-example/) – Vijay Ram Mar 31 '20 at 08:38

3 Answers3

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;
                }
            }
        });
  • 21
    get span size method determines the amount span width your cell is going to take not the number of col row should have !! – Karthik Rk Dec 23 '15 at 02:01
  • 1
    when spanning the first item, it's messing up the height of the next ones. It works on any other item. Any idea ? – Ronny Shibley Jun 18 '17 at 15:40
  • 1
    @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 required – Umair Mar 07 '18 at 07:35
  • 1
    This is not woking. – Mahdi Oct 24 '18 at 13:11
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;
                }
    }
});
Android Developer
  • 9,157
  • 18
  • 82
  • 139
  • How comes no one links to the actual documentation: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup 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. – Bruno Bieri Nov 15 '21 at 17:19
2

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

Rugved Mahamune
  • 546
  • 1
  • 5
  • 8