RecyclerView GridLayoutManager with full width header

17,335

Try with this:

mLayoutManager = new GridLayoutManager(this, 2);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.HEADER:
                        return 2;

                    case MyAdapter.ITEM:
                    default:
                        return 1;
                }
            }
        });

And check these links:

Share:
17,335
Kibi
Author by

Kibi

Programmer

Updated on June 21, 2022

Comments

  • Kibi
    Kibi about 2 years

    I'm using a very helpful example here to show a RecyclerView and a GridLayoutManager to show a grid with a header.

    It looks pretty good, but my graphic designer wants the header item to take up the full width of the RecyclerView. Right now there is padding.

    When I set up the GridLayoutManager I add in padding (which I still want for the other grid items): [it's using Xamarin C#]

    var numColumns = myListView.MeasuredWidth / tileSizeMax;
    myGridView.SetPadding(tilePadding, tilePadding, tilePadding, tilePadding);
    layoutManager = new GridLayoutManager(Activity, numColumns);
    myListView.SetLayoutManager(layoutManager);
    

    So, how can I set the padding to be different for the header item...or make it draw itself over the padding?

  • JSong
    JSong over 7 years
    You saved my day! mAdapter.getItemViewType(position) was the very line I desperately needed!
  • Mahdi
    Mahdi over 5 years
    how to handle item decorator for last item?
  • Denny
    Denny over 5 years
    I guess we'll never know
  • bovquier
    bovquier almost 5 years
    @Kenji as a hint, you can check: gitlab.com/snippets/1895752 it has a view reference, you have its position, so you can modify it using some of your ideas ;)
  • Pankaj Kant Patel
    Pankaj Kant Patel about 3 years
    Great solution, Saved my life :)