Android notes-listview Summary (multiple choice box listviiew, dynamic loading, multi-thread update progress bar in listview)

Source: Internet
Author: User

Http://www.cnblogs.com/chon/archive/2011/06/28/2092317.html

Why listview?

If the listview is not necessary only for functional purposes, the scrollview can basically be used for the tasks that listview can do. The most fundamental reason for the existence of listview is its efficiency (how to implement it ?). Listview reduces memory consumption by reusing objects, and also reduces the CPU consumption by creating objects (creating view objects in androidk is often accompanied by parsing XML ). The essence of listview is a bitmap (of course, all the control texts that are viewed on the screen will eventually become Bitmap). listview will follow the needs, based on the information provided by the adapter, the required item is displayed on the screen. When the screen is scrolled, the location of the item is recalculated and a new bitmap is displayed on the screen. This may not sound very efficient, but the advantage is that every use is the first item.Create a view object. objects with the same style can share a view object, reducing memory consumption. In addition, listview is event-driven. It will be re-drawn only when necessary and onlyDraw the items displayed on the current screen.

How to use?

Listview cannot be separated from the adapter. Generally, a class is created to inherit methods such as baseadapter, override getcount (), and getview. Generate the object of this class and call setadapter () of listview to bind it with listviw.

How does it work?

Listview will call the getcount () method of the adapter bound to it to know how many items need to be displayed, and then call getview (INT position, view convertview, viewgroup parent) cyclically) know how to draw the position item and draw it until the current listview space is filled up. When the data in the adapter changes, you need to call yydatasetchanged
() Tell the adapter that the data has changed or register an observer registerdatasetobserver (datasetobserver observer) for the adapter ). When the adapter learns that the bound data has changed the time, it will call the getcount () method again, and call getview (INT position, view convertview, viewgroup parent) cyclically to refresh the current page.

Item 1

Item 2

Item 3

Item 4

Item 5

Item 6

Item 7

Item 8

When this listview needs to create an item9 to scroll up, some objects (such as Item1) will not be visible in the display area, then android will set the Item1
The reference is passed to the adapter. convertview in getview () so that we do not need to create another view to store item9. We only need to modify the original Item1 object to reuse it; we don't have to worry about whether convertview is of the correct type. This is guaranteed by the system. So what we need to do is to convert convertview into our own view and assign it a value, in
This case :( textview) convertview. settext ("item9 ");

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//this would be first time to show the item,so we need to create it
convertView = mInflater.inflate(R.layout.item, null);
}
//we grab the convertView,modify it and reuse it
((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]);
return convertView;
}

The consumption of calling a method is much higher than the access variable, and the above Code calls the findviewbyid () method again and again to do the same thing. So we can further optimize it as follows: Create a class to save some view references, so that we can use it directly without calling findviewbyid (). because all we save is that the reference is not the object itself, you don't have to worry about occupying a large amount of memory.

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, 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();
}
//we store the reference ,so that we don’t have to call findViewById() over and over again
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}

It seems that the data collected on T-Mobile G1 is not very accurate now, but the performance difference is of great reference value.

Tips & tricks

Listview is designed to display large data volumes. If the amount of data (the number of items) is not large and it is troublesome to use listview, you may wish to use scrollview instead of listview.

If the layout of item information is complex or the number of items is large, we recommend that you customize a view component to implement the required functions, rather than combining other controls to achieve the desired effect.

Listview scrolling to Black: add an attribute Android: cachecolorhint = "#00000000" to listview in XML ". If the listview contains many items, you sometimes need to scroll quickly. For example, when you scroll from the first item to the second item, many items in the middle do not mean much to the user, but Android calls the adapter. the getview () method draws these items one by one, and the user does not want any delay because the scrolling is very fast. This is very difficult for low-end mobile phones such as G1. Therefore, one way Google engineers come up with is to make the screen black and use a black bitmap to cover the listview while scrolling, instead of drawing many items in the intermediate process, thus improving performance.

Item has its own background covering the selector cursor: add an attribute to listview in XML: Android: drawselectid Top = "true" so that the cursor will run to the layer on the item, solve our problem.

Snippets

Multiple choice box listview

For listview with progress bars, multiple sub-threads refresh their respective progress. If there are many sub-threads, refresh will become very frequent. We can use a handler to refresh them in a unified manner, in this way, we need to add some additional conditions to limit the number of refresh times and conditions.

The principle of batch loading is very simple. Add an onscrolllistener to the listview to listen for Rolling events. When the user scrolls to a specific position on the screen, load new data and add a footerview to the listview, when the data loading ends, remove the footerview.

If you have any questions, you may find bugs, have better ideas or suggestions, or cannot download attachments. Please send email to arthurbrown@163.com

Engineering source code

QQ: 304811527

Emal: arthurbrown@163.com

Android QQ group: 172448270

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.