Practice and principle analysis of single refresh method for Android listview

We are not familiar with the method of using listView to refresh with adapter. First we refresh the data in adapter, then we call notifydatasetchange to notify listView to refresh the interface.

Although the method is simple, it involves an efficiency problem. Calling notifydatasetchange will actually cause the getview method of adapter to be called many times (how many times can be displayed on the screen). If it is clear that only the data of an item in the list has been updated (for example, the user clicks an item in the list to update the display status of the item, or the background callback updates an item in the list, etc.), try to avoid multiple innocent calls to getview, especially when there are too many background threads, the callback frequency is particularly high, and the layout of the interface is not optimized very well, use notitydatasetchaned() Method to update the interface, it will appear that the list is stuck and the user experience is poor.

Let's introduce how to refresh a listview:

First, let's take a look at the getview method of the adapter. If we want to refresh a single message, we need to call this method manually.

How to determine these three parameters? The third parameter is your listview.

In order to determine the other two parameters position and convertview, here are several new methods of lisview:

Getfirstvisibleposition(), this method obtains the position of the first visible item in the list in the current state.

Getlastvisibleposition(), this method obtains the position of the last visible item in the list in the current state.

Getitematposition (int position). This method returns the convertview of listview at position in the current state

PS: the convertview here is reusable, that is, no matter how big the position value is (it depends on how big your whole list is), the number of convertviews should always be as many as the number of lists that can be displayed on the screen.

Therefore, we compare the listitem from the value of getfirstvisibleposition to the value of getlastvisibleposition and the conditions (such as ID) to be updated to determine which one is to be updated (if it is not within the current range, it is not necessary to update, and it will be updated naturally when the list is pulled)

The code is as follows. In fact, this method is the method proposed at Google's 2011 Developer Conference - listview single update:

The above is the practice and principle analysis of Android listview single refresh method introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>