cannot resolve setSpanSizeLookup for gridviewlayoutmanager

1k views Asked by At

I am attempting to set a custom span size depending on the type of object that i want to display, however when i am getting the following error in the IDE:

cannot resolve method SetSpanSizeLookup(anonymous.android.support.v7.widget.GridLayoutManager.SpanSizeLookup)

I can't figure out why this is as it appears to be a supported method according to google

mLayoutManager = new GridLayoutManager(this,3);

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;
        }
    }
});
1

There are 1 answers

0
johnsky On BEST ANSWER

Your mLayoutManager object must of be of type GridLayoutManager.

In the case where you need it to be of the abstract type LayoutManager (even though I cannot envision such a case), you can cast it as follows:

((GridLayoutManager) mLayoutManager).setSpanSizeLookup(...)