This question has already been answered:

I want to make a universal implementation of RecyclerView for different types of View.

To do this, you need to determine the type of the view that needs to be drawn at the moment. Where does the viewType parameter in this method come from and what values ​​can it take?

  @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 

Reported as a duplicate by members 0xdb , Community Spirit ♦ Jul 21 '18 at 7:42 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    it is set in the adapter when creating a ViewHoldera. First google examples in English, then ask. This question is duplicated many times. - George Chebotaryov

1 answer 1

The viewType parameter in the onCreateViewHolder() method returns the override adapter method getItemViewType() . In this method, you must organize the logic for determining the type of item by position in the list (input argument of the method).
If the method is not overridden in the adapter, then the parameter returns 0 for all positions.

The parameter can be any integer of type int , you specify the specific value yourself in the logic of the method. The number only serves to identify the type of item.

Usage example