Android ListView Adapter (Adapter) Optimization method _android

Source: Internet
Author: User
Tags static class

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!

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.