How to optimize android listview

Source: Internet
Author: User

This article only summarizes several methods of optimizing the view in getView, just like several methods of writing the word "" written by Kong Yiji. Do not spray it or laugh, just share it with others, if you have any mistakes, please correct them. Thank you.
Listview
Aviewthatshowsitemsinaverticallyscrollinglist.
A list view that displays a vertical scroll subitem. In android development, there are many places where listview is used to display data and form a vertical view. Using listview is a standard adapter mode. The data is displayed as needed by the data --, interface -- xml, and adapter -- adapter. xml describes how the data is displayed, activities.
If a custom adapter is used, the getView method will be rewritten to generate the view and data for the item in the getView method.
See the figure below:

Here there is an optimization point, that is, reusing the view, which reduces memory consumption and accelerates item loading.
The Optimization in getView is very common. I have summarized the Three optimization methods below. please correct me.
First:
ConvertView is reused, greatly reducing memory consumption. If you determine whether convertView is null, You need to generate a view. Then, you can return the View data to the bottom layer and present it to the user.
Features: If the current convertView is null, a view is generated through LayoutInflat. Copy codeThe Code is 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:
The preceding statement has a disadvantage: each time you get a view, you need to re-find the findViewById, and then assign values to the control and set the event accordingly. In this case, we are actually doing the same thing, because the geiview actually contains these controls, and the IDs of these controls are the same, that is, as long as the findViewById is in the view, findViewById is not required each time.
The second method is provided below.
The write feature usually has an internal class classViewHolder. This ViewHolder is used to identify some controls in the view to facilitate the setting of Event-related operations, such as onClick, in this way, you do not need to use findViewById every time, reducing the performance consumption. At the same time, convertView is reused, which greatly reduces memory consumption.Copy codeThe Code is 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:
I personally think this writing method is the most comfortable. The most comfortable way is to look at the code with a very nice and clear look.
Feature, uses the internal class classViewHolder, reuse convertView.
The second difference is that a temporary variable Viewview = convertView is used, the view is modified, and the view is returned.Copy codeThe Code is 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 a concentrated Writing Method for beginners to learn and summarize.
Source code: LisViewTest.zip
According to the suggestions provided by friends downstairs, there are still some optimizations. The latest updates are as follows:Copy codeThe Code is 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;
}

Note:: StaticclassViewHolder
Here, ViewHolder is set to static, that is, static. static classes only take a long time to load for the first time, but they can be well loaded later, at the same time, it ensures that there is only one ViewHolder in the memory, saving the memory overhead.
Thank you for your suggestions and attention!

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.