2

The purpose of a ViewHolder pattern is to help recycle views. What if in my list I have different layout for each and every row. In my case I have a GridView with 6 elements in two rows. Each element has completely different layout and presents something else. Is there any reason I should still implement ViewHolder in this case?

Booyaches
  • 1,669
  • 4
  • 27
  • 40

2 Answers2

2

Calling findViewbyId() method all the time for referencing Widgets in the layout slows down app performance considerably. For smooth scrolling of the ListView or RecyclerView, ViewHolder needs to be used which reduces the referencing time and helps in smooth scrolling.

According to your question, if you have different layout for each row then you need to create separate ViewHolder objects for each layout. Then, at the time when you inflate data into it, you'll need to identify which ViewHolder you need to use for the current position. This might make your code a little bit complex but, it considerably improves app performance.

For more info on ViewHolder, visit the following link: Hold View Objects in a View Holder

Varun Kumar
  • 1,241
  • 1
  • 9
  • 18
  • see http://daniel-codes.blogspot.com/2013/11/is-findviewbyid-slow.html, `findViewbyId` does not `"slow down app performance considerably"` - in fact none of google's `android.widget.ListAdapter`s use view holder pattern – pskink Apr 21 '16 at 07:38
  • Check the link to the docs I have provided in the answer above. In the docs, it is written that findViewById() slows down app performance. – Varun Kumar Apr 21 '16 at 08:27
  • so tell me if google guys dont care about the performance as none of their ListAdapters implement that pattern? – pskink Apr 21 '16 at 08:49
  • I'm not saying anything like that @pskink. I'm sorry if I have offended you or anyone else. I'm very sorry. I apologize to anyone who reads this post or anyone working at Google. I was just saying that I read it in the docs and post it in the answer. – Varun Kumar Apr 21 '16 at 09:58
  • 1
    To anyone who reads this answer: If I have offended anyone in this post, I'm very sorry. I apologize for anything that I have said above. I respect everyone, Google and the people who work at Google. I might have not been clear above and maybe some people take it the wrong way. All I want to say is that whatever I have learned in Android has been either from docs provided on Android Developers website or from the Android Developers channel on YouTube. I read about findViewById in the docs provided and mentioned it in the answer connected to this comment. – Varun Kumar Apr 21 '16 at 10:10
  • My intention here was just to share my knowledge that I have gained from the docs itself. I didn't intend to offend anyone especially the people at Google. And regarding the comment above, I just wanted to tell that my answer came from the docs itself. Maybe I was not clear enough that @pskink took it the other way. I'm sorry once again if I have offended you. – Varun Kumar Apr 21 '16 at 10:19
  • you didnt offend anyone i think, basically wheather you use holder pattern or not doesnt make any noticable impact on the performance nowdays, maybe it was important in android 1.* era but not today – pskink Apr 21 '16 at 10:52
  • The view holder pattern does make a difference if you want 60 FPS while flinging with very complicated row items. That's why they made `RecyclerView`. – Kane O'Riley Apr 21 '16 at 16:30
  • To add to my previous statement, on the N preview on a Nexus 6P, frame rendering time on a fling for a particular list went from 7ms to 30ms when I disabled view holder reuse. For slow movements it's fine but you'll notice it on flings if you have more than just a couple of views in each row. – Kane O'Riley Apr 21 '16 at 16:36
1

ListView is guaranteed to pass in the correct convertview for that type of data when you override geitemviewtype. GetTag will also work properly with multiple view types and will still prevent calling getItemById all the time thus increasing performance. So in my opinion: yes you should still use the viewholder pattern.

jobbert
  • 3,297
  • 27
  • 43