Android App series: A collection of Viewholder tool class implementations

Source: Internet
Author: User
Tags knowledge base

Article Source: http://www.zretc.com/technologyDetail/459.html

  Objective

In the process of developing the app, the siege lion must be with the ListView, GridView these components flirt, dark send a few waves of glances. Natural original Ecological Beauty Baseadapter is the programmer's favorite, with it, we want to do how to do, hey, you know haha ~

However, every time we write a baseadapter, we are very conscious of writing to him a viewholder, one or two is OK, in case the application of countless ListView, hehe ~ Your sister! The same, see all the aesthetic fatigue. As the greatest of the 22nd century of the procedures, take off, engage in forever is our most sincere pursuit, so we how to remove Viewholder from the Baseadapter? is not not not, but to make it into a gorgeous tool class implementation, income corner that lonely tools class.

  The realization of Viewholder

I think we should briefly introduce the implementation of the Viewholder, Google is very clever in the adapter use of the idea of re-use view, naturally let our cock silk machine can also bubble some white rich beauty application a little bit more likely. The implementation of Viewholder is basically embodied in the Baseadapter getView (int position, View Convertview, ViewGroup Parent) In this method, see the following code:

1. @Override

2. Public View GetView (int position, View Convertview, ViewGroup parent) {

3. Viewholder Holder;

4. if (Convertview = = null) {

5. Convertview = Inflater.inflate (R.layout.listview_item_layout, parent, false);

6. Holder = new Viewholder ();

7. Holder.studentname = (TextView) Convertview.findviewbyid (r.id.student_name);

8. Holder.studentage = (TextView) Convertview.findviewbyid (r.id.student_age);

9. Convertview.settag (holder);

10.}

One. else {

Holder = (viewholder) convertview.gettag ();

13.}

Student data = (Student) getItem (position);

Holder.studentName.setText (Data.getname ());

Holder.studentAge.setText (Data.getage ());

. return Convertview;

18.}

19.

Class Viewholder {

. public TextView Studentname;

Public TextView Studentage;

23.}

Obviously, people do not ask me where viewholder, a little look up to help to see the big class Viewholder. The Viewholder usage here mainly has two places, one is the reuse of the Convertview, and the other is the reuse of the index in the Convertview viewholder. Specific usage is not familiar with the words can Baidu, and then said sorry I am today this blog post, because here to write this code purpose, certainly not introduce you how to use Viewholder, just want to tell you: the traditional viewholder writing, is how bloated! And for every new Baseadapter, you have to be bored to realize it again and again, oh~

  Viewholder's Tool class implementation

Naturally, the naked should be small, action to early. Since we are bored, we will write it into a tool class. See the code below

1. Static Class Viewholder {

2. public static T Get (view view, int id) {

3. Sparsearray Viewholder = (sparsearray) view.gettag ();

4. if (Viewholder = = null) {

5. Viewholder = new Sparsearray ();

6. View.settag (Viewholder);

7.}

8. View Childview = viewholder.get (ID);

9. if (Childview = = null) {

Ten. Childview = View.findviewbyid (ID);

One. Viewholder.put (ID, Childview);

12.}

Return (T) Childview;

14.}

15.}

This is the implementation of the tool class, a little bit of the implementation of the principle:

1, Viewholder since is dependent on the view of the tag storage, but with a Sparsearray collection storage.

2, Judge the view in the tag whether there is viewholder, does not exist, hurriedly call her to have a.

3, then in Viewholder (that is, Sparsearray) look for the index of the view, if not, hurriedly Findviewbyid a put in the way to return, if already exist, happy, direct use Bai.

Put a baseadapter inside the code used:

1. @Override

2. Public View GetView (int position, View Convertview, ViewGroup parent) {

3. if (Convertview = = null) {

4. Convertview = Inflater.inflate (R.layout.listview_item_layout, parent, false);

5.}

6. TextView name = Tools.ViewHolder.get (Convertview, r.id.student_name);

7. TextView age = Tools.ViewHolder.get (Convertview, r.id.student_age);

8.

9. Student data = (Student) getItem (position);

Ten. Name.settext (Data.getname ());

One. Age.settext (Data.getage ());

12.

return convertview;

14.}

Concise and clear, do not say ~ ~ Hey, back if you want to write Viewholder, direct Tools tool class call, worry not waste brain.

  Analysis Feasibility

Since we are using it as a tool class, it is necessary to evaluate the value of this tool before we use it.

In general, we can evaluate from the following aspects: ease of use? Memory leaks? Performance improvement? Robustness? And so on ...

Ease of Use: The greatest feature of the tool class is ease of use, this viewholder is a typical use of the doctrine, we do not have to worry about writing some of the appropriate code, directly into the view and ID, cohesion loose coupling. and adopt the method of generic template of T, automatically adapt to the external view sub-class, do not need us to force the reload manually.

Memory leaks: Some beginners, see the static method back to the stubborn think Sparsearray viewholder This variable will have a memory leak, but Java tells us that the variable's life is only in the method execution, the method is complete, GC recovery The existence viewholder, as always, is placed in the view tag, once the view is recycled, viewholder disappears naturally. Do not believe, open Ddms, with your 28 youth hand-Stop brush the ListView to try to ensure that the object is basically stable in a value.

Performance boost: Here we find that using Sparsearray this set instead of HASHMAP, we know that Sparsearray is a tool class for Android and is officially recommended to replace the HashMap class, Its internal use of the two-point search to improve the efficiency of the search, and not a little bit two oh, who use who know, the specific content want to understand, can degree Niang brother or left the source.

So, as a tool class, it is fully qualified, and quickly copy it into your tools, util, and then we can be happy with the elegance of pleasure with viewholder.

Learn more about the basics of Android development, tool applications, coding rules, etc. welcome to the technical knowledge base of soft international education Group!

Android App series: A collection of Viewholder tool class implementations

Related Article

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.