How to optimize the ListView list used in Android development Viewholder

Source: Internet
Author: User

In Android development, you will inevitably encounter a lot of data loaded into the ListView to display,

Then one of the most important data-passing bridges adapter adapters are commonly used, as the market needs

Find the content in the ListView ' entry is getting more and more this requires programmers to customize the adapter,

And the key is the adapter optimization problem, the adapter is not optimized often will cause oom

(memory overflow) or sliding lag, I'll introduce you to a common

Adapter optimization method for the use of

1   /**2 * Adapter for List view3      */4     classAdapter extends Baseadapter {5 6         /**7 * Returns the total number of item Entries8          *9 * @returnTen          */ One @Override A          Public intGetCount () { -             returnnewsinfos.size (); -         } the  -         /** - * The secondary method returns an object type and can return any type of data - * What I'm returning here is a Ben object in the Java Ben Collection +          * - * @param position + * @return A          */ at @Override -          PublicObject GetItem (intposition) { -             returnNewsinfos.Get(position); -         } -  -         /** in * Returns the location of item, counting starting from 0 -          * to * @param position + * @return -          */ the @Override *          Public LongGetitemid (intposition) { $             returnposition;Panax Notoginseng         } -  the         /** + * Define a Viewholder object A          */ the Viewholder Holder; +  -         /** $ * This method returns a view of item $          * - * @param the location of the item in the position ListView - * @param Convertview This convertview is actually the most critical part of the principle is that when the ListView sliding process will have the item slide out of the screen and no longer be used when Android will reclaim the view of this item This view is the Convertview here. the * When ITEM1 is removed we will re-new view to the newly displayed item_new and if we use this convertview we can actually reuse it so as to save the new view A lot of overhead - * @param The parent of parent is the parents view of item's view, the less commonly used to adjust the item widthWuyi * @return the          */ - @Override Wu          PublicView GetView (intposition, View Convertview, ViewGroup parent) { -  About             if(Convertview = =NULL) { $Convertview = View.inflate (homeactivity. This, R.layout.list_item,NULL); -             } -              -             //It's not all about optimization, it's all about setting up data for each control . AHolder =Getholder (convertview); +Holder.com.setText (Newsinfos.Get(position). Getcomment () +"Times Viewed"); theHolder.type.setText (GetType (Newsinfos.Get(position). GetType ())); -Holder.time.setText (Newsinfos.Get(position). GetTime ()); $Holder.desc.setText (Newsinfos.Get(position). GetDescription ()); theHolder.title.setText (Newsinfos.Get(position). GetTitle ()); theString Timeid = Newsinfos.Get(position). GetId (); theString stats =shareprefenceutils the. getinstance (homeactivity. This). GETSTR ("New_item"+ Timeid,""); -             if(Textutils.equals (Timeid, stats)) { inHolder.stats.setText ("is read"); theHolder.stats.setSelected (true); the}Else { AboutHolder.stats.setText ("not read"); theHolder.stats.setSelected (false); the             } theHOLDER.ICON.SETIMAGEURL (Newsinfos.Get(position). GetImage (),NULL); +             returnConvertview; -         } the Bayi         /** the * Get Viewholder the          * - * @param view - * @return the          */ the          PublicViewholder getholder (view view) { the             //get holder by passing in the view's Gettag method theViewholder holder =(Viewholder) View.gettag (); -             if(Holder = =NULL) { the                 //If the view passed in does not have a tag set, re-holder the new instance theHolder =Newviewholder (view); the                 //set tag for the view passed in94 View.settag (holder); the             } the             //Back to Holder the             returnHolder;98         } About  -         /**101 * Define a Viewholder class102          */103         classViewholder {104             //the property controls defined below are all controls in the item in the ListView the TextView title;106 TextView desc;107 TextView time;108 TextView type;109 TextView com; the TextView stats;111 smartimageview icon; the 113             /** the * When creating an instance of this class, you need to pass in the view of item the * To get an instance of each control in item the              *117 * @param view118              */119              PublicViewholder (view view) { -title =(TextView) View.findviewbyid (r.id.tv_title);121icon =(Smartimageview) View.findviewbyid (R.id.iv_icon);122desc =(TextView) View.findviewbyid (R.ID.TV_DESC);123Time =(TextView) View.findviewbyid (r.id.tv_time);124Type =(TextView) View.findviewbyid (r.id.tv_type); thecom =(TextView) View.findviewbyid (r.id.tv_com);126Stats =(TextView) View.findviewbyid (r.id.tv_stats);127             } -         }129}

Finally, let's see what I realized. If you have friends who are interested in the item style, please leave a message below and I'll write an article about custom styles in Android

How to optimize the ListView list used in Android development Viewholder

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.