Background (you probably know this, but just in case): the adapter contains a collection of objects and uses the information from these objects to populate the views (each view is a position in the list). The list view displays a display of these views. For performance reasons, ListView will recycle views that are no longer visible as they scroll at the top or bottom of the list. Here's how to do it:
When the ListView needs a new view to display, it calls the getView adapter with the integer argument "position" to indicate which object in the adapter collection it wants to see (the position is just a number from 1 to N -1), where N is the number of objects in the adapter.
If he has any views that are no longer visible, he will pass one of them to the Adapter too, like "convertView". It says "reusing this old view, not creating a new one." Big gain in performance.
The code in the article attaches a ViewHolder to each view created by it, which, among other things, contains the position of the object requested by the ListView. In the code of the article, this position is hidden inside the ViewHolder along with a pointer to the field inside the view that will contain the image. ViewHolder is attached to the view as a tag (separate topic).
If the view is redesigned to hold another object (in a different position), the ListView will call Adapter.getView (newPosition, oldView ...) The code in the article will save the new position in the ViewHolder attached to the oldView (guess so far?) And start loading it A new image to put it in the view.
Now in this article, he launches AsyncTask to retrieve the data that must go into the view). This task has a position (from a call to getView) and a holder (from an oldView). The position indicates what data was requested. The owner tells you what data is currently to be posted in this view, and where it can be placed as soon as it appears.
If the view is redesigned again while AsyncTask is still running, the position in the holder will be changed so that these numbers do not match, and AsyncTask knows that the data is no longer needed.
Is this clearer?
Dale wilson
source share