android ListView Optimization, in the Android project, in the use of the ListView interface and data display, this time if the resources are too large, for the project, the user experience is certainly bad, here on how to optimize the detailed introduction:
The role of adapter is the bridge between the ListView interface and the data, when each item in the list is displayed to the page, the adapter GetView method is invoked to return a view. Did you ever think about it? What will it look like when we have 1000000 items in our list? Is it taking up a lot of system resources?
The role of the ListView adapter is shown in the following illustration:
Let's look at the following code:
Public View GetView (int position, view Convertview, ViewGroup parent) {
View item = minflater.inflate (r.layout.list_i Tem_icon_text, null);
((TextView) Item.findviewbyid (R.id.text)). SetText (Data[position));
((ImageView) Item.findviewbyid (R.id.icon)). Setimagebitmap ((
position & 1) = = 1? micon1:micon2);
return item;
}
What do you think? If more than 1000000 items, the consequences of unimaginable! You mustn't write like that!
Let's take a look at the following code:
Public View GetView (int position, View Convertview, ViewGroup parent) {
if (Convertview = null) {
Convertview = m Inflater.inflate (R.layout.item, null);
((TextView) Convertview.findviewbyid (R.id.text)). SetText (Data[position));
((ImageView) Convertview.findviewbyid (R.id.icon)). Setimagebitmap ((
position & 1) = = 1? micon1:micon2);
return convertview;
}
How, the above code is not a lot better? The system will reduce the creation of many view. Performance has been greatly improved.
Is there any way to optimize it? The answer is yes:
Public View GetView (int position, View Convertview, ViewGroup parent) {
Viewholder holder;
if (Convertview = = null) {
Convertview = minflater.inflate (R.layout.list_item_icon_text, null);
Holder = new Viewholder ();
Holder.text = (TextView) Convertview.findviewbyid (r.id.text);
Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon);
Convertview.settag (holder);
} else {
holder = (viewholder) convertview.gettag ();
}
Holder.text.setText (Data[position]);
Holder.icon.setImageBitmap ((position & 1) = = 1 micon1:micon2);
return convertview;
}
Static class Viewholder {
TextView text;
ImageView icon;
}
What do you think? Will it bring a great improvement to your system? Look at the following three ways to compare the performance of the diagram you will know!
Thank you for reading, I hope to help you, thank you for your support for this site!