0

I have a very big recyclerView in my code with a lot of if statements in the onBindViewHolder: sometimes position 1 to 5 will have this view, sometimes they will have another view, and some other times, they will have another views. I have more than 20 positions in some cases and some positions have sometimes 3 variants.

Can I create a class for each specific variant and then write something like

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
   if (variant 1) {
      Variant1Class.onBindViewHolder(holder, position)
   }
   else if (variant 2) {
      variant2Class.onBindViewHolder(holder, position)
   }
   else {
     variant3Class.onBindViewHolder(holder, position
   }

}

This way I can split the logic in more than one file. Is it problematic to pass the "holder" variable around ? Is it problematic for some other reason ?

If you have another solution, I'm all ears!

Best

EtienneG
  • 13
  • 2
  • yes, you can add separate files for View Holder when you can bind your items. – Waqar UlHaq Mar 01 '20 at 16:16
  • Thank you for your answer Waqar, can I have separate files for onCreateViewHolder and onBindViewHolder too ? – EtienneG Mar 01 '20 at 16:21
  • please have a look to this tutorial. Where he created separate class for viewholder https://medium.com/@droidbyme/android-recyclerview-with-multiple-view-type-multiple-view-holder-af798458763b – Waqar UlHaq Mar 01 '20 at 16:28
  • This is a great tutorial for beginners, but it doesn't help me with very big adapters :D – EtienneG Mar 01 '20 at 18:46

0 Answers0