Android ApiDemos example resolution (181): Views-& gt; Lists-& gt; 14.Eff

Source: Internet
Author: User

In the previous example, temporary data is used to bind List items to solve the performance problems of List items during List scrolling during loading. This example describes how to compile an efficient List Adapter, in fact, it is not a special technology, mainly as follows:

Reuse getView to input the convertView parameter to avoid expanding the View from XML multiple times.
A ViewHolder is designed to store a TextView and ImageView, so as to avoid calling findViewById every time.
In the end, variables are used to save some objects that originally needed to call inflate or findViewById multiple times. When you call getView again next time, you can reuse these objects to improve the getView efficiency.


[Java]
Public View getView (int position,
View convertView, ViewGroup parent ){
// A ViewHolder keeps references to children
// Views to avoid unneccessary CILS
// To findViewById () on each row.
ViewHolder holder;
 
// When convertView is not null, we can reuse
// It directly, there is no need
// To reinflate it. We only inflate a new View when
// The convertView supplied
// By ListView is null.
If (convertView = null ){
ConvertView = mInflater
. Inflate (R. layout. list_item_icon_text,
Null );
 
// Creates a ViewHolder and store references
// To the two children views
// We want to bind data.
Holder = new ViewHolder ();
Holder. text
= (TextView) convertView. findViewById (R. id. text );
Holder. icon
= (ImageView) convertView. findViewById (R. id. icon );
 
ConvertView. setTag (holder );
} Else {
// Get the ViewHolder back to get fast
// Access to the TextView
// And the ImageView.
Holder = (ViewHolder) convertView. getTag ();
}
 
// Bind the data efficiently with the holder.
Holder. text. setText (DATA [position]);
Holder. icon. setImageBitmap (position & 1) = 1
? MIcon1: mIcon2 );
 
Return convertView;
}
 
Static class ViewHolder {
TextView text;
ImageView icon;
}

Public View getView (int position,
View convertView, ViewGroup parent ){
// A ViewHolder keeps references to children
// Views to avoid unneccessary CILS
// To findViewById () on each row.
ViewHolder holder;

// When convertView is not null, we can reuse
// It directly, there is no need
// To reinflate it. We only inflate a new View when
// The convertView supplied
// By ListView is null.
If (convertView = null ){
ConvertView = mInflater
. Inflate (R. layout. list_item_icon_text,
Null );

// Creates a ViewHolder and store references
// To the two children views
// We want to bind data.
Holder = new ViewHolder ();
Holder. text
= (TextView) convertView. findViewById (R. id. text );
Holder. icon
= (ImageView) convertView. findViewById (R. id. icon );

ConvertView. setTag (holder );
} Else {
// Get the ViewHolder back to get fast
// Access to the TextView
// And the ImageView.
Holder = (ViewHolder) convertView. getTag ();
}

// Bind the data efficiently with the holder.
Holder. text. setText (DATA [position]);
Holder. icon. setImageBitmap (position & 1) = 1
? MIcon1: mIcon2 );

Return convertView;
}

Static class ViewHolder {
TextView text;
ImageView icon;
}

 

Here, at least the convertView passed in by getView of List Adapter is reused, instead of creating a new convertView each time.

 

 

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.