Android-ViewHolder-optimized ListView

Source: Internet
Author: User

Android-ViewHolder-optimized ListView

In Android development, ListView is an important component. It displays specific content in a list based on the data length. You can freely define the layout of each column of ListView.
When the listview has a large amount of data to be loaded, it will occupy the memory and affect the performance. At this time, it needs to be filled as needed and re-use the View to reduce object creation.
The data loaded by ListView is carried out in the getView () method. At the same time, you need to customize the ListView to override ListAdapter, such as BaseAdapter, SimpleAdapter, CursorAdapter, etc.
Optimize the ListView loading speed. Make convertView match the list type and reuse convertView to the maximum extent.

GetView () can be loaded in the following ways:
(1) slowest Loading Method: redefines a View each time, loads the layout, and loads data.

    public View getView(int position, View convertView, ViewGroup parent) {        View item = mInflater.inflate(R.layout.list_item_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;    }

(2) fast loading method: When convertView is not empty, convertView is reused directly to reduce unnecessary View creation and then load data.

    public View getView(int position, View convertView, ViewGroup parent) {        if (convertView == null) {            convertView = mInflater.inflate(R.layout.item, parent, false);        }        ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]);        ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap(                (position & 1) == 1 ? mIcon1 : mIcon2);        return convertView;    }

(3) The fastest way: Define a ViewHolder and set the tag of convetView to ViewHolder. If it is not empty, use ViewHolder.

    static class ViewHolder {        TextView text;        ImageView icon;    }    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder;        if (convertView == null) {            convertView = mInflater.inflate(R.layout.list_item_icon_text,                    parent, false);            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;    }

Comparison of efficiency in three modes:

When loading time-consuming resources, perform the following operations to make loading faster and smoother:
(1) modify the adapter in the main thread of the interface
(2) data can be obtained anywhere, but data should be requested in another place
(3) Submit the adapter changes in the main interface thread and call the yydatasetchanged () method (update the data source)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.