Android listview asynchronously loads image misplacement, repetition, blinking analysis, and solutions
When we use listview to asynchronously load images, problems such as misplacement, repetition, and blinking may occur when the image is quickly slide or the network is poor. In fact, these problems are summarized as a problem,
For example, if the listview contains 100 items and only 10 items are displayed on a screen, we know that converView () in getView () is used to reuse the view object, because the view object of an item, the imageview control is obtained through findViewById (). When we reuse the view object, this imageview is also reused, for example, if the view of 11th items reuse the first itemv iew object, the imageview is reused at the same time, and the display image is controlled by imageview, so when the image is not downloaded, the data displayed in this imageview is the reused data.
1: The item image is displayed repeatedly?
This duplicate display indicates that the item in the current row displays the image of the item in the previous row.
For example, if ListView slides to 2nd rows, an image is asynchronously loaded, but loading is slow. During the loading process, listView slides to 14th rows, and the image loading ends,
Rows 2nd are no longer on the screen. According to the cache principle described above, 2nd rows of view may be reused by 14th rows. In this way, we can see that 14th rows show pictures that should belong to 2nd rows, duplicate display
Row item picture display disorder?
This display disorder means that a row of items displays images that do not belong to the row of items .?
For example, if ListView slides to 2nd rows, an image is asynchronously loaded, but loading is slow. During the loading process, listView slides to 14th rows, and 2nd rows are no longer on the screen,
According to the cache principle described above, 2nd rows of view may be reused by 14th rows, and 14th rows will display 2nd rows of View. When the previous image loading ends, it will be displayed in 14th rows, confusion
Is the row item picture flickering?
In the case of B above, the loading of 14th rows of images soon ended. Therefore, we can see that 14th rows of images first showed 2nd rows of images, and immediately showed that their images were overwritten, causing flickering disorder.
Solution:
Through the above analysis, we know that the reason for confusion is asynchronous loading and Object reuse.
GetView
You can give the object an identifier,
Compare the ID with the current row when asynchronous loading is complete
Item
Are the same,
I
It is displayed, otherwise it will not be processed.
Through the above analysis, we know that the reason for confusion is asynchronous loading and Object reuse.
GetView
You can give the object an identifier,
Compare the ID with the current row when asynchronous loading is complete
Item
Are the same,
I
It is displayed, otherwise it will not be processed.
Through the above analysis, we know that the reason for confusion is asynchronous loading and Object reuse. If each getView operation can give an object an identifier, when asynchronous loading is complete, whether the comparison identifier is consistent with the identifier of the item in the current row is displayed. Otherwise, no processing is performed.
Principle: first, set a tag for the imageview. The url is set in the tag. Then, compare the url obtained during loading with the url in the position to be loaded. If the url is different, load the image, the same is that the previous ones are reused and will not be loaded.