Wednesday 31 March 2021

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.)



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

No comments:

Post a Comment