GridLayoutManager setSpanSizeLookup not working
Asked Answered
D

2

9

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?

Deel answered 30/1, 2017 at 4:17 Comment(0)
E
20

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);
}
Extortioner answered 30/1, 2017 at 4:25 Comment(3)
I want to be at even position with 2 spans and and odd position with 1 span, but the two spans only gets one to be shown and leaves the grid space for the second column only empty! I want all positions to behave like that not a specific oneGeneticist
@NaszNjokaSr.just modify getItemViewType if condition should be if(position%2 != 0){ return 1}; rest of code same as aboveExtortioner
it will span 1 column at all odd positionExtortioner
R
3

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);
}
Rib answered 22/9, 2017 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.