I am using GridLayoutManager with 2 cells and for some cells I want span to be one so I tried using setSpanSizeLookup but its not working. I tried returning span count 1 for all positions but still two cells are appearing instead of one.
Following is my code
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return 1;
        }
    });
    recyclerView.setLayoutManager(gridLayoutManager);
Any reasons why it is not working?
Replace
return 1;
to
return 2;
This specify you are spaning 2 cells into 1 cell.
Code
Here is my code for spaning 2 cell for specific position
GridLayoutManager glm=new GridLayoutManager(mContext,2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch(categoryAdapter.getItemViewType(position)) {
                case 1:
                    return 2;
                default:
                    return 1;
            }
        }
    });
    mRecyclerViewCategory.setLayoutManager(glm);
How to define case span in your Recycler Adapter
@Override
public int getItemViewType(int position) {
    if(position==[your_specific_postion_where_to_span]){
        return 1;
    }
    return super.getItemViewType(position);
}
i struggled with this as the documentation here is poor at this time. I figured it out like this...
getSpanSize and getSpanIndex seem to work together.   For me i was trying to insert a pageViewer inside a gridlayoutManager that spaned two columns.  so it was defined like: mGridLayout = new GridLayoutManager(getActivity(), 2);
//must be called before setLayoutManager is invoked
private void setNumOfColumnsForPageViewer(final FallCollectionRecyclerAdapter adapter) {
    mGridLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (adapter.getItemViewType(position) == MyRecyclerAdapter.TYPE_PAGE_VIEWER)
                return 2; //some other form of item like a header, or whatever you need two spans for
            else
                return 1; //normal item which will take up the normal span you defined in the gridlayoutmanager constructor
        }
        @Override
        public int getSpanIndex(int position, int spanCount) {
            if (adapter.getItemViewType(position) == FallCollectionRecyclerAdapter.TYPE_PAGE_VIEWER)
                return 1;//use a single span
            else
                return 2; //use two spans
        }
    });
    mRecyclerView.setLayoutManager(mGridLayout);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With