Android ListView Optimization of several ways to explain _android

Source: Internet
Author: User
This article is just a summary of the getview inside the optimization of the views of several ways, like the anise beans to write the anise bean several ways of writing the same, master do not spray, do not laugh, just take out to share, there is the wrong place welcome to correct, thank you.
ListView
Aviewthatshowsitemsinaverticallyscrollinglist.
A list view that displays a vertical scroll subkey in Android development, there are a lot of places to use ListView, using it to show data as a vertical view. Using ListView is a standard adapter pattern, with data--, interface--xml, and adapter--adapter--The data is presented as needed by the adapter, XML describes how the data is presented, and activity controls these activities.
Where you use custom adapter, you will override the GetView method, where the GetView method produces a view of the user item and the data.
See figure:

Here's an optimized place to reuse view, which reduces memory consumption while speeding up item loading.
GetView in the optimization of places, we must be very situation, the following I summed up three kinds of optimization, please correct me.
First:
Reuse of Convertview, largely reduced memory consumption. By judging whether Convertview is null, it is necessary to produce a view, then give the view data, and finally return the view to the bottom, presented to the user.
features: If the current convertview is null, a view is generated through Layoutinflat.
Copy Code code as follows:

Viewcode
Publicviewgetview (intposition,viewconvertview,viewgroupparent)
{
if (convertview==null)
{
Convertview=layoutinflater.from (context). Inflate (R.layout.section_list_item1,null);
}
Textviewtv_name= (TextView) Convertview.findviewbyid (r.id.contact_contactinfoitem_tv_name);
Textviewtv_phone= (TextView) Convertview.findviewbyid (r.id.contact_contactinfoitem_tv_phonenum);
Contactinfo1confo=contacts.get (position);
if (confo!=null) {//toseteveryitem ' stext
Tv_name.settext (Confo.getcontactname ());
Tv_phone.settext (Confo.getcontact_phone ());
}
Returnconvertview;
}

Second:
One disadvantage of the above writing is that each time you getview, you need to Findviewbyid again, find the control again, and then assign the control and set the event accordingly. This is actually doing repetitive things, because of the Geiview, actually contains these controls, and the IDs of these controls are the same, that is, in fact, as long as in the view Findviewbyid once, there is no need to findviewbyid every time after.
Here's a second way of writing.
The characteristics of the writing, there is usually an internal class classviewholder, this viewholder, used to identify some of the controls in the view, to facilitate the setting of some events corresponding operation, such as onclick and so on, so that you do not have to findviewbyid every time, Reduce the cost of performance. At the same time, reuse of Convertview, to a large extent, reduce the memory consumption.
Copy Code code as follows:

Viewcode
Publicviewgetview (Intposition,viewconvertview, Viewgroupparent)
{
Viewholderholder;
if (convertview==null) {
Convertview=layoutinflater.from (context). Inflate (R.layout.section_list_item1, NULL);
Holder=newviewholder ();
Holder.tv_name= (TextView) Convertview.findviewbyid (r.id.contact_contactinfoitem_tv_name);
Holder.tv_phone= (TextView) Convertview.findviewbyid (r.id.contact_contactinfoitem_tv_phonenum);
Convertview.settag (holder);
}
Else
{
holder= (viewholder) Convertview.gettag ();
}
Contactinfo1confo=contacts.get (position);
Log.i ("My", "Confo" +confo.getcontactname ());
if (confo!=null) {//toseteveryitem ' stext

Holder.tv_name.setText (Confo.getcontactname ());
Holder.tv_phone.setText (Confo.getcontact_phone ());
}
Returnconvertview;
}
Classviewholder
{
Textviewtv_name,tv_phone;
}

Third:
Personally feel that this writing is the most comfortable, the most comfortable meaning is looking at the code is a very cool, see very clear.
Features, the use of internal class classviewholder, reuse of Convertview.
The second way to do this is to use a temporary variable viewview=convertview, and then modify view, and finally return to view
Copy Code code as follows:

Viewcode
@Override
Publicviewgetview (intposition, Viewconvertview,viewgroupparent)
{
Viewview=convertview;
Viewholderholder;
if (view==null) {
View=layoutinflater.from (context). Inflate (R.layout.section_list_item1,null);
Holder=newviewholder ();
Holder.tv_name= (TextView) View.findviewbyid (r.id.contact_contactinfoitem_tv_name);
Holder.tv_phone= (TextView) View.findviewbyid (r.id.contact_contactinfoitem_tv_phonenum);
View.settag (holder);
}
Else
{
holder= (viewholder) View.gettag ();
}
Contactinfo1confo=contacts.get (position);
Log.i ("My", "Confo" +confo.getcontactname ());
if (confo!=null) {//toseteveryitem ' stext

Holder.tv_name.setText (Confo.getcontactname ());
Holder.tv_phone.setText (Confo.getcontact_phone ());
}
Returnview;
}
Classviewholder
{
Textviewtv_name,tv_phone;
}

The above is the concentrated writing, for beginners to learn and summary.
The source code is as follows: Lisviewtest.zip
According to the advice of friends downstairs, we find that there are some optimization places, the latest updates are as follows:
Copy Code code as follows:

Viewcode
@Override
Publicviewgetview (intposition,viewconvertview,viewgroupparent)
{
Viewview=convertview;
Viewholderholder;
if (view==null) {
View=layoutinflater.from (context). Inflate (R.layout.section_list_item1,null);
Holder=newviewholder ();
Holder.tv_name= (TextView) View.findviewbyid (r.id.contact_contactinfoitem_tv_name);
Holder.tv_phone= (TextView) View.findviewbyid (r.id.contact_contactinfoitem_tv_phonenum);
View.settag (holder);
}
Else
{
Holder= (Viewholder) View.gettag ();
}
Contactinfo1confo=contacts.get (position);
LOG.I ("My", "Confo" +confo.getcontactname ());
if (confo!=null) {//toseteveryitem ' stext

Holder.tv_name.setText (Confo.getcontactname ());
Holder.tv_phone.setText (Confo.getcontact_phone ());
}
Returnview;
}
<fontcolor= "\" #0000ff \ "" ></font>staticclassviewholder
{
Textviewtv_name,tv_phone;
}

Attention: Staticclassviewholder
Here set the Viewholder as static, that is, statically, static classes will only take a long time to load the first time, but later can be very good to help load, while ensuring that there is only one viewholder in memory, saving the cost of memory.
Thank you very much for your suggestions and our attention!

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.