Create Multiple ViewHolders in RecycerView

Rohit Singh
1 min readMay 26, 2019

--

Creating a Recyclerview with multiple view types could be very easy* (Terms and conditions apply)

What does it look like?

We can create this in a single RecyclerView. Just follow these 3 steps

  1. Override public int getItemViewType(int position)
  2. Return different ViewHolders based on the ViewType in onCreateViewHolder() method
  3. Populate View based on the itemViewType in onBindViewHolder() method

Here is a small code snippet

getItemViewType(int position) is the key

Many developers don’t know that this method exists because it is not an abstract method which makes them(including me) wonder where to begin. Once you know that this method exists, creating such RecyclerView would be a cakewalk.

Lets see one example to prove my point. Eg. If you want to show two layouts at alternate positions.

Do this.

Relevant Links:

Check out the PROJECT where I have implemented this.

Putting up content takes a lot of time and effort 😓.
👏 clap 👏 If you think you learned something from this article.
You can find me on Stackoverflow,Github,and,on Linkedin.
Give feedback and feel free to correct mistakes.

--

--

Rohit Singh
Rohit Singh

Written by Rohit Singh

Android Developer | Arizona State University | NASA Psyche Research Aide

No responses yet