What is the Viewholder of "Android development experience"? Static Final Static final?

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

Now we all know to use viewholder to achieve the optimization of the ListView, but, viewholder exactly what to use to decorate it? What is the point of this modification? In a ListView, how many instances of Viewholder exist? Why can viewholder reduce the number of findview and optimize efficiency? In order to understand this problem, I did the following test, the test code is very simple, is the following

public class Mainactivity extends Activity {public static int itemId = 1; @Overrideprotected void OnCreate (Bundle savedinst Ancestate) {super.oncreate (savedinstancestate); ListView ListView = New ListView (this); Listview.setadapter (new Myadapter ()); Setcontentview (listview); Private class Myadapter extends Baseadapter {@Overridepublic int getcount () {return 50;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Viewholder = null;if (convertvi EW = = NULL) {Convertview = Getlayoutinflater (). Inflate (r.layout.item,parent, false); Viewholder = new Viewholder (); Viewholder.tv = (TextView) Convertview.findviewbyid (r.id.tv); Convertview.settag (Viewholder);} else {Viewholder = (Viewholder) Convertview.gettag ();} ViewHolder.tv.setText ("item" + position); LOG.D ("TAG", "position=" + position + "---" + viewholder.tostring ()); return Convertview;}}Private class Viewholder {//private static class Viewholder {//private final class Viewholder {//private static final CLA SS Viewholder {private int id;public Viewholder () {id = itemId; LOG.D ("TAG", "Viewholder" + ID); itemid++;} TextView TV; @Overridepublic String toString () {return "--------id=" + ID;}}

Let me briefly say what the code means, Viewholder has a member variable ID, used in ToString (), to distinguish between different viewholder, in the constructor, the ID is assigned, itemid is a static variable, each initialization of the + 1, we can calculate the number of instances of Viewholder based on the number of times the constructor is printed, and according to ToString () it is possible to determine which viewholder to use. The notation in GetView is fixed, and the following is the test result:


Private class Viewholder decorated, the interface is visible within the number of item is 9, Viewholder initialized 10 times, then began to reuse Viewholder.


private Static class Viewholder decorated, the interface is visible within the number of item is 9, Viewholder initialized 10 times, and then began to reuse Viewholder.


Private Final class Viewholder decorated, the interface is visible within the number of item is 9, Viewholder initialized 10 times, then began to reuse Viewholder.


Private static final class Viewholder decoration, exactly the same!



In fact, I put a picture on the OK, because these kinds of exactly the same, no matter what decoration, Viewholder will initialize the current interface to see the number of item + 1 times, and the Convertview number of instances is the same, so I do not use any decoration is not a problem, is not necessary.

static modifier class, here is the static inner class, is not to say that there is only one instance, but can access the static variables of the external class, the final decorated class is not allowed to inherit the class, we use final no basis here, so, later write Viewholder, can not tangle, Add what add Ah, do not add anything!

What is the Viewholder of "Android development experience"? Static Final Static final?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.