Create Multiple ViewHolders in RecycerView
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
- Override
public int getItemViewType(int position)
- Return different ViewHolders based on the ViewType in
onCreateViewHolder()
method - 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.