This article assumes familiarity with creating a RecyclerView using a single ViewHolder for each item, which is the most common use case.
Here are the possible cases where a RecyclerView might use an adapter with multiple ViewHolders instead:

Case One
Have one RecyclerView to display different layouts for different items at the same time, based on certain conditions. Just as an example, here I have a RecyclerView that uses two different layouts for each item based on odd/even position.

Case Two
Have one RecyclerView to display the same layout for each item. However, based on user action on certain conditions (such as during sorting or filtering action), the same RecyclerView will display the same data using a different layout. As an example, here’s a RecyclerView that uses just one layout for each item. Use your own imagination for an example where the RecyclerView at a different time uses a different layout instead.
Case 1
Here’s the general method. I’m only listing areas that differ from the usual case of RecyclerView with just one ViewHolder:
- We now have to create the multiple ViewHolder classes first. I prefer them to be inner classes of the custom RecyclerView.Adapter class. Pretty self-explanatory, the only possible gotcha/oddity here is that inside the class’s constructor, we create variables that directly access specific views inside a certain layout, without the class knowing about which layout it is related to.
- The RecyclerView adapter now must extend the more generic
RecyclerView.Adapter<RecyclerView.ViewHolder>
class instead of using the single custom ViewHolder class as the generic type. - The RecyclerView now must override
getItemViewType()
, which is used to decide which ViewHolder class to be used on a certain item position in the RecyclerView. - The method
onCreateViewHolder()
must now return the more genericRecyclerView.ViewHolder
- The method
onBindViewHolder()
must now use the more genericRecyclerView.ViewHolder
object as its first parameter.
Case 2
- Have a variable to determine which ViewHolder should be used by the RecylerView. This will usually be inside the fragment or activity.
- Pass that variable as one of the parameters for constructing the RecyclerView’s adapter.
- Inside the adapter, create the multiple ViewHolder classes first. I prefer them to be inner classes of the custom
RecyclerView.Adapter
class. Pretty self-explanatory, the only possible gotcha/oddity here is that inside the class’s constructor, we create variables that directly access specific views inside a certain layout, without the class knowing about which layout it is related to. - The RecyclerView adapter now must extend the more generic
RecyclerView.Adapter<RecyclerView.ViewHolder>
class instead of using the single custom ViewHolder class as the generic type. - The RecyclerView adapter now must override
getItemViewType()
, and check the variable from step 2 to determine which ViewHolder to use. - The method
onCreateViewHolder()
must now return the more genericRecyclerView.ViewHolder
- The method
onBindViewHolder()
must now use the more genericRecyclerView.ViewHolder
object as its first parameter.