What is Viewholder Pattern?

Asked

Viewed 1,772 times

8

I’m implementing a RecyclerView on Android and I noticed that it is necessary to create a class called ViewHolder. This class seems to be a design standard called Viewholder Pattern, and my doubts arise exactly at this point.


Doubts

  1. What is Viewholder Pattern?
  2. What is the purpose of the Viewholder Pattern?
  3. What are your benefits?

1 answer

11


What is Viewholder Pattern?

Approach used to store a set of views so they can be efficiently accessed and reused when needed.
The purpose of the class Viewholder is to save the views.

What is the purpose of the Viewholder Pattern?

Avoid repeated use of findViewById() for references to views.
When stored in a Viewholder object references are available immediately.

What are your benefits?

Avoid loss of performance due to repeated use findViewById().

  • A @ramaral question, Butterknife has a similar function that avoids repetition of calling findViewById()... So we can say that he is within this pattern ?

  • @Matheus No. What Butterknife does is generate, for yes, the method calls findViewById(). See the main page of Butterknife. There, when referred to the Viewholder standard, it is said that it "simplifies", which is different from replacing.

  • @ramaral, speaking of Butterknife, using the Viewholder standard on an Activity, and passing it as a parameter to an auxiliary class, is it bad practice regarding efficiency? or would it be better to pass the objects on themselves? or it would be even better to do everything you have to do in the views within Activity itself (whether to use viewholder or not)?

  • @GR_ I’m sorry but I can’t understand what you’re asking, perhaps due to lack of context. What do you mean, "use the Viewholder pattern in an Activity"?

  • @ramaral I’ll create a question and leave the link here for you to understand. I find it easier to explain everything in a haha comment

  • @ramaral https://answall.com/q/270541/4306

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.