Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in ViewHolder pattern should the ViewHolder class be static?

Tags:

I am just trying to have a better understanding of the following pattern I regularly use to optimize ListView

My readings only pointed me to the fact that a static inner class is treated as top level class. What is the benefit of such a thing compared to a member class (non static)?

@Override public View getView(int position, View convertView, ViewGroup parent) {     Comment comment = getItem(position);     ViewHolder holder;     if (convertView == null){         holder = new ViewHolder();         convertView = LayoutInflater.from(context).inflate(R.layout.mylayout, null);         holder.nickname = (TextView) ((ViewGroup) convertView).findViewById(R.id.nickname);         convertView.setTag(holder);     }else{         holder = (ViewHolder) convertView.getTag();     }      holder.nickname.setText(comment.getMember_nickname());     CharSequence     return convertView; }  public static class ViewHolder{     TextView nickname; } 
like image 366
znat Avatar asked Nov 22 '12 16:11

znat


People also ask

What is the ViewHolder pattern Why should we use it?

The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent call of findViewById() during ListView scrolling, and that will make it smooth.

What is ViewHolder class in Android?

ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.

What is the use of onBindViewHolder?

bindViewHolder. This method internally calls onBindViewHolder(ViewHolder, int) to update the RecyclerView. ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView.


2 Answers

My opinion is that it is better to have the ViewHolder class static as it won't leak the Adapter.

If the adapter retains some heavy Collections or even Views (depends on each particular case), it would be great to keep control of which objects retain the Adapter.

Having a lot of objects instances of an inner class will have those objects referencing the Adapter, thus retaining it. You should be careful about how the tags are managed (if the views are cleaned/removed automatically there is no problem>).

like image 97
DNax Avatar answered Jan 20 '23 21:01

DNax


One benefit of using static inner class, is that the inner class can be accessed from static methods, without having an instance of the outer class.

If the inner class non-static:

class MyOuter {     private int x = 7;     public void makeInner() {         MyInner in = new MyInner();         in.seeOuter();     }     class MyInner {         public void seeOuter() {             System.out.println("Outer x is " + x);         }     } }  public static void main(String[] args) {     MyOuter mo = new MyOuter();     MyOuter.MyInner inner = mo.new MyInner();     inner.seeOuter(); } 

If the inner class is static:

class BigOuter {     static class Nest {void go() { System.out.println("hi"); } } }  class Broom {     static class B2 {void goB2() { System.out.println("hi 2"); } }     public static void main(String[] args) {         BigOuter.Nest n = new BigOuter.Nest();         n.go();         B2 b2 = new B2();         b2.goB2();     } } 
like image 30
Illyes Istvan Avatar answered Jan 20 '23 21:01

Illyes Istvan