Android ListView Single Refresh method practice and Principle analysis

Source: Internet
Author: User

For the method of refreshing with the ListView with adapter, everyone is not unfamiliar, first refresh the data in adapter, and then call Notifydatasetchange notify the ListView refresh interface.

Although the method is simple, but this involves an efficiency problem, calling Notifydatasetchange actually causes the Adpter GetView method to be called multiple times (how many times the screen can be displayed), If it is clear that only update the list of the data of an item (such as the user to click on the list of an item to update the display state of the item, or the background callback to update the list of an item, etc.), should try to avoid getview be innocent multiple calls, especially when the background thread is particularly many, The callback frequency is particularly high, and the layout of the interface is not optimized to be particularly good, the use of the notitydatasetchaned () method to update the interface will appear as a list of lag, the user experience is poor.

Let's look at a single refresh of the ListView:

First we look at adapter's GetView method, we need to do a single refresh to call this method manually.

Public View GetView (int position, view Convertview, ViewGroup parent)

So how do these three parameters be determined, and the third parameter is OK, and that's your listview.

To determine the other two parameters position and Converview, here are a few new ways to Lisview:

Getfirstvisibleposition () that gets the position of the first visible item of the list under the current state.

Getlastvisibleposition () that gets the position of the last visible item of the list under the current state.

getitematposition (int position) that returns the Convertview of the ListView at position position in the current state

PS: Here the Convertview is reused, that is, no matter how big the value of position (this depends on how big your whole list), the number of Converview should always be the screen can be displayed on the list of the number of so many.

So, we pass the listitem between the value of getfirstvisibleposition and the value of getlastvisibleposition and need to enter The criteria for the row update (for example, id) are compared to determine which one is to be updated (if not in the current scope there is no need to update, and when the list is pulled, it will be updated naturally)

The code below, in fact, this method is the Google 2011 Developer conference proposed method--listview a single update:

Private voidUpdatesinglerow (ListView ListView,LongID) {if(ListView! =NULL) {             intStart =listview.getfirstvisibleposition ();  for(inti = start, j = Listview.getlastvisibleposition (); I <= J; i++)                 if(id = =( (Messages) listview.getitematposition (i)). GetId ()) {View View= Listview.getchildat (I-start);                     GetView (i, view, ListView);  Break; }         }     }
/*** ListView Single Update *@paramDownvo*/    Private voidUpdatesinglerow (Downvo downvo) {if(ListView! =NULL){            intStart =listview.getfirstvisibleposition ();  for(intI=start, J=listview.getlastvisibleposition (); i<=j;i++)                if(downvo==listview.getitematposition (i)) {View View= Listview.getchildat (I-start);                    Adapter.getview (i, view, ListView);  Break; }        }    }

Android ListView Single Refresh method practice and Principle analysis

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.