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.