javaandroidandroid-recyclerviewandroid-adaptergridlayoutmanager

How to get the item view type of a SectionedRecyclerViewAdapter, properly?


I use this code to give header items in a RecyclerView a span of two columns using a GridLayoutManager:

MyAdapter myAdapter = new MyAdapter(this, items);
this.myRecyclerView.setAdapter(myAdapter);

GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        switch (myAdapter.getItemViewType(position)) {
            case -1: // VIEW_TYPE_ITEM
                return 1;
            default: // VIEW_TYPE_HEADER
                return 2;
        }
    }
});
this.myRecyclerView.setLayoutManager(layoutManager);

This works, but it complains about my usage of getItemViewType, because it's deprecated. My adapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder>.

What should I use, instead?

(Also, I don't know about the enum that I should use in the switch statement, instead, but this is a different story.)


Solution

  • you have to use recyclerview viewpool great way to do section recyclerview like google play its inbuilt functionality of recyclerview.

    eg of section recyclerview -

    https://www.youtube.com/watch?v=EyUjw6b5gXE

    https://github.com/alghifari/RecycledViewPoolExample