For the use of ListView with adapter to refresh the way everyone is not unfamiliar, first refresh the data in the adapter, and then call Notifydatasetchange notify ListView Refresh interface.
Although the method is simple, but it involves an efficiency problem, calling Notifydatasetchange in fact will lead to Adpter GetView method is called multiple times (the screen can show how many times will be called), If it is clear that only the data for one item in the list is updated (for example, when a user clicks on a list item and then updates the item's display state, or the background callback updates a list item, and so on), should try to avoid getview to be innocent of multiple calls, especially when the background thread is particularly many, Callback frequency is particularly high, and the interface layout optimization is not particularly good time, using the notitydatasetchaned () method to update the interface will appear list of cotton, user experience is poor.
Here's how to do a single refresh of ListView:
First we take a look at the adapter 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 are these three parameters determined, and the third parameter is pretty sure, that's your listview.
To determine the other two parameters position and Converview, here are a few lisview new methods:
Getfirstvisibleposition (), which gets the position of the first visible item of the list in the current state.
Getlastvisibleposition (), which gets the position of the last visible item in the current state of the list.
getitematposition (int position), which returns ListView Convertview at the position position in the current state
PS: The convertview here is reused, that is to say, no matter how big the position value is (this depends on how big your entire list is), the number of Converview should always be the number of bars on the list that can be displayed on the screen.
So, we pass from Getfirstvisibleposition value to getlastvisibleposition value between the ListItem and the need to enter The criteria for row updates (such as IDs) are compared to determine which one is to be updated (if it is not in the current scope there is no need to update, etc list pull when the natural will be updated)
The code is as follows, in fact this method is the Google 2011 Developers conference proposed by the method--listview single update:
private void Updatesinglerow (ListView ListView, long id) {
if (ListView!= null) {
int start = LISTVIEW.GETFIRSTVI Sibleposition ();
for (int i = start, j = listview.getlastvisibleposition (); I <= J; i++)
if (id = = (Messages) listview.getitematpos Ition (i)). GetId () {
View view = Listview.getchildat (I-start);
GetView (i, view, ListView);
break;
}
}
The above is a small set to introduce the Android ListView single Refresh method practice and principle analysis, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!