How Android optimizes the ListView

Source: Internet
Author: User


As we all know, in the development process, the optimization of the ListView is more important

So, how to optimize it?


The improvement efficiency of the ListView is in fact the optimization of the GetView method in adapter, so how to make the GetView optimization?


1, the reuse of Convertview in GetView () , to a large extent, reduce the consumption of memory. By judging if Convertview is null,

Yes, you need to generate a view, then give the view data, and finally return the view to the bottom and present it to the user.  

For example: The ListView screen shows 10 data, when the user slides the ListView, when the 11th data appears, the first piece of data

The recycler, which is provided by Android itself, puts the item (View) into the RecycleBin (Recycle Bin) and displays the new view

Image, repeat this view from the RecycleBin. There is no need to create new view every time, and save a lot of resources.


2, self write a viewhold class, this viewholder, used to identify some of the controls in the view, to facilitate the setting of the corresponding operation of some events ,

so you don't have to the View of GetView () just need to find the control once, not every time to find


The code is represented as follows:

Optimization of the GetView method

public View getView (int position, View Convertview, ViewGroup parent) {
if (Convertview = = null) {
Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (
r.layout.activity_list_item, NULL);
viewholder Viewholder = new Viewholder (Convertview);
Convertview.settag (Viewholder);
}
viewholder Viewholder = ( Viewholder) Convertview.gettag ();
Viewholder.fillview (GetItem (position));
return convertview;
}

This class is the display of the view, which is the display of each item in the ListView.

Class Viewholder {
Private ImageView iv_newsimage;
Private TextView Tv_newstitle;
Private TextView tv_newsdate;


Public Viewholder (View Convertview) {
Iv_newsimage = (ImageView) Convertview.findviewbyid (r.id.newimage);
Tv_newstitle = (TextView) Convertview.findviewbyid (r.id.newtitle);
Tv_newsdate = (TextView) Convertview.findviewbyid (r.id.newdate);
}


public void Fillview (Object item) {
Final Newsinfo news = (newsinfo) item;
Tv_newstitle.settext (News.getnewtitle ());
Tv_newsdate.settext (GetDate (News.getnewdate ()));
Imageloader.getinstance (). DisplayImage (News.getnewimageurl (),
Iv_newsimage);
}


Private String GetDate (long time) {
Date date = new Date (time);
SimpleDateFormat formatter = new SimpleDateFormat (
"Yyyy-mm-dd HH:mm:ss");
return Formatter.format (date);
}
}


How Android optimizes the ListView

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.