ListView Summary (Multi box listviiew, dynamic load, multi-threaded update progress bar in ListView)

Source: Internet
Author: User

Why ListView?

ListView if only because of the functional requirements of the ListView may not exist, the things that the ListView can do are basically scrollview capable. The most fundamental reason for a ListView is its efficiency (how is it implemented?) ). The ListView reduces memory consumption by reusing objects and also reduces the amount of CPU consumed by creating objects (creating view objects in ANDROIDK is often accompanied by parsing XML). The essence of the ListView is a bitmap (of course all control text will eventually become bitmap on the screen), and the ListView will, according to the requirements, draw the required item to the screen based on the information provided by adapter. When the screen scrolls, it recalculates the position of the item and draws a new bitmap display on the screen. This may not sound very efficient, but the advantage of this is that each view object created with a single item can have a view object shared by a style object, reducing memory consumption. And the ListView is event-driven and is redrawn only when needed, and only the items displayed on the current screen are drawn.

How do I use ?

The ListView is inseparable from adapter, and the usual practice is to create a class that inherits Baseadapter,override GetCount () and GetView () methods. Generates an object of this class, calling the ListView Setadapter () to bind to LISTVIW.

How does Does It work?

The ListView calls the GetCount () method of the adapter that binds it to know how many item needs to be shown, and then loops through the getview (int position, View Convertview, ViewGroup parent) Know how to draw the position item and draw it until the current ListView is filled with space. When the data in the adapter changes, call notifydatasetchanged () to tell adapter that the data has changed or to register an observer with adapter Registerdatasetobserver ( Datasetobserver observer). When adapter learns that the data it is bound to has changed time, the GetCount () method is called again, and the current page is refreshed by calling GetView (int position, View Convertview, ViewGroup parent).

Item1
Item2
Item3
Item4
Item5
Item6
Item7
Item8

When this listview  scroll up needs to create a item9  at the same time, android  will item1   Reference passed to  adapter.getview ()   Convertview so we don't have to create a view to store the ITEM9, just need to change the original Item1 object, you can re-use  ; we don't have to worry convertview  is not the correct type, this is guaranteed by the system, so we have to do is to put Convertview  conversion (often requires downward transformation) into our own view  to assign value to it, in This case: (TextView) Convertview.settext ("Item9");

 public  View getView (int   position, View Convertview, ViewGroup parent) { if  (Convertview = = null   // this would is first time to show the item,so we need to create it  Convertview = Minflater.inflate (r.layout.    Item, null  );  //  ((TextView) Convertview.findviewbyid (R.id.text)). SetText (data[    Position]);  return   Convertview;}  

Calling a method consumes much more than an access variable, and the above code calls the Findviewbyid () method again and again, doing something repetitive. So we can further optimize the following: Create a class to hold some view references so that we can use them directly without having to call Findviewbyid (). Because we're saving just the reference not the object itself, so don't worry about consuming a lot of memory

Static classViewholder {TextView text; ImageView icon;} PublicView GetView (intposition, View Convertview, ViewGroup parent)    {Viewholder holder; if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item_icon_text,NULL); Holder=NewViewholder (); 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, we don ' t has to call Findviewbyid () over and over againHolder.text.setText (data[position]); Holder.icon.setImageBitmap ((Position& 1) = = 1?micon1:micon2); returnConvertview;}

The data collected on the T-mobile G1 may not be accurate now, but the difference in performance is of great reference value.

Tips & Tricks

The ListView is designed for large-volume data presentation. If the amount of data (the number of item) is not very large, and using the ListView to achieve more trouble, it is possible to change the idea, do not use the ListView, and use ScrollView to achieve.

If the item information layout is complex or the number of item is many, for performance reasons, it is recommended to customize a view component to implement the required functionality, rather than combining other controls to achieve the desired effect.

ListView scrolling Black: Adds an attribute android:cachecolorhint= "#00000000" to the ListView in XML. When there are many item in the ListView, it is sometimes necessary to scroll quickly. For example, when scrolling from the first item to the NO. 600 item this time, a lot of item in the middle is not very significant to the user, but Android calls the Adapter.getview () method to draw the item one by one, and the user does not want any delay because the scrolling is very fast. This is hard to do in some low-end handsets like G1. So Google engineers came up with a way to make the screen black with a black bitmap to cover the ListView instead of drawing a lot of item in the middle of the process, to improve performance.

Item has its own background that covers the selector cursor: Add a property to the ListView in XML: Android:drawselectorontop= "True" so the cursor will go to the layer above the item and solve our problem.

Snippets

Multiple marquee ListView

A ListView with a progress bar, many sub-threads refresh their progress, if a lot of sub-threads so the refresh will become very frequent, we can be a handler responsible for unified refresh, so we have to add some additional conditions to limit the number of refreshes and conditions

The principle of batch loading is simple, adding a Onscrolllistener listener scrolling event to the ListView, loading the new data when the user scrolls to the screen to a specific location, and adding a loaded Footerview to the ListView. This footerview is removed when the loading data is finished.

ListView Summary (Multi box listviiew, dynamic load, multi-threaded update progress bar in ListView)

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.