Android App series: A collection of Viewholder tool class implementations

Source: Internet
Author: User

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 O (∩_∩) o 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:

@Override PublicView GetView (intposition, View Convertview, ViewGroup parent)    {Viewholder holder; if(Convertview = =NULL) {Convertview= Inflater.inflate (r.layout.listview_item_layout, parent,false); Holder=NewViewholder (); Holder.studentname=(TextView) Convertview.findviewbyid (r.id.student_name); Holder.studentage=(TextView) Convertview.findviewbyid (r.id.student_age);    Convertview.settag (holder); }    Else{Holder=(Viewholder) Convertview.gettag (); } Student Data=(Student) getItem (position);    Holder.studentName.setText (Data.getname ());    Holder.studentAge.setText (Data.getage ()); returnConvertview;}classViewholder { PublicTextView Studentname;  PublicTextView studentage;}

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

Static classViewholder { Public Static<textendsView> T Get (view view,intID) {Sparsearray<View> Viewholder = (sparsearray<view>) View.gettag (); if(Viewholder = =NULL) {Viewholder=NewSparsearray<view>();        View.settag (Viewholder); } View Childview=viewholder.get (ID); if(Childview = =NULL) {Childview=View.findviewbyid (ID);        Viewholder.put (ID, Childview); }        return(T) Childview; }}

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:

@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {    if(Convertview = =NULL) {Convertview= Inflater.inflate (r.layout.listview_item_layout, parent,false); } TextView name=Tools.ViewHolder.get (Convertview, r.id.student_name); TextView Age=Tools.ViewHolder.get (Convertview, r.id.student_age); Student Data=(Student) getItem (position);    Name.settext (Data.getname ());        Age.settext (Data.getage ()); returnConvertview;}

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 using the <t extends view> T Generic template method, automatically with the external View sub-class adaptation, do not need us to manually force the reload.

memory leaks: Some beginners, see the static method back to the stubborn think sparsearray<view> viewholder This variable will have a memory leak, but Java tells us that this variable's life is only in the method of execution, The method is complete, GC is recycled; The presence Viewholder is always placed in the view tag, and 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 collection instead of HashMap, we know that Sparsearray is a tool class for Android and is officially recommended to replace hashmap< Integer,e> of a class, its internal use of two points to find the implementation of improved search efficiency, 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, quickly copy it to your tools, util inside, and then we can be happy with the elegance of pleasure with viewholder.

Enjoy wind chimes
Source: http://www.cnblogs.com/net168/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original connection, or next time not to you reproduced.

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.