Analysis and solution of dislocation, repetition and flicker of listview asynchronous loading pictures in Android
Android listview asynchronously loads image dislocation, repetition, flicker analysis and solutions. Please see the following for specific problem analysis and solutions.
When we use listview to load pictures asynchronously, in the case of fast sliding or poor network, there will be problems such as picture dislocation, repetition and flicker. In fact, these problems are summarized as a problem. We need to optimize listview for these problems.
For example, there are 100 items on the listview, and only 10 items are displayed on one screen. We know that convertview in getview() is used to reuse view objects, because an item corresponds to a view object, and the ImageView control is obtained by the view object through findviewbyid(). When we reuse the view object, this ImageView object is also reused. For example, if the view of the 11th item multiplexes the first item view object, the ImageView is multiplexed at the same time. Therefore, when the picture is not downloaded, the data displayed by the ImageView (11th item) is the data of the multiplexed (1st item).
1: Item picture display duplicate
This display repetition means that the current line item displays the picture of a previous line item.
For example, when the listview slides to line 2, a picture will be loaded asynchronously, but the loading is very slow. During the loading process, the listview has slid to line 14, and the picture loading ends during the sliding process. Line 2 is no longer on the screen. According to the caching principle described above, the view object in line 2 may be reused by line 14. In this way, we can see that the picture that should belong to line 2 is displayed in line 14, resulting in repeated display.
2. The item picture is displayed disorderly
This display disorder means that a line item displays a picture that does not belong to the line item.
The same reason as above.
3. Item picture display flashes
In the other case described above, if the picture in line 14 is loaded soon, we see that line 14 first displays the reused picture in line 2, and immediately displays its own picture for coverage, resulting in flicker disorder.
Solution:
Through the above analysis, we know that the reason for the disorder is asynchronous loading and object reuse. If getview can give an identification to the object each time, and compare whether the identification is consistent with the identification of the item in the current line when asynchronous loading is completed, it will be displayed. Otherwise, it will not be processed.
Principle: first set a tag for the ImageView. In this tag, the URL of the image is set. Then, when loading, obtain the URL and compare it with the URL in the position to be loaded. If it is different, it will be loaded. If it is the same, it will be reused, and the previous one will not be loaded.
Android displays pictures in listview (repeated disorder and flashing problem)
1. Cause analysis
Listview item caching mechanism:
For better performance, the listview caches the line item (the view corresponding to a line).
Listview obtains the item of each row through the getview function of the adapter.
During sliding
a. If an item in a line has slipped out of the screen, if the item is not in the cache, put it into the cache, otherwise update the cache;
b. Before getting the line item that slides into the screen, it will first judge whether there is an available item in the cache. If so, it will be passed to the getview of the adapter as the convertview parameter.
In this way, the following getview writing method can make full use of the cache and greatly improve the performance of listview. Even if there are tens of thousands of line items, the maximum number of inflates is n,
N is the maximum number of listview line items displayed on one screen.
This improves performance, but also causes other problems:
a. Duplicate line item picture display
This display repetition means that the current line item displays the picture of a previous line item.
For example, when listview slides to line 2, a picture will be loaded asynchronously, but the loading is very slow. During the loading process, listview has slid to line 14, and the picture loading ends,
Line 2 is no longer on the screen. According to the caching principle described above, the view in line 2 may be reused by line 14, so what we see is that line 14 displays the picture that should belong to line 2,
Cause duplicate display.
b. The line item picture is displayed disorderly
This display disorder means that a line item displays a picture that does not belong to the line item.
For example, when the listview slides to line 2, a picture will be loaded asynchronously, but the loading is very slow. During the loading process, the listview has slid to line 14, and line 2 is no longer on the screen. According to the caching principle described above, the view in line 2 may be reused by line 14, and line 14 displays the view in line 2. At this time, the previous picture will be displayed on line 14 after loading, causing confusion.
c. Line item picture display flashes
In the case of B above, the picture in line 14 will be loaded soon, so we see that the picture in line 2 is displayed in line 14 first, and our own picture is displayed immediately for coverage, resulting in flicker disorder.
2. Solution
Through the above analysis, we know that the reason for the disorder is asynchronous loading and object reuse. If getview can give an identification to the object each time, and compare whether the identification is consistent with the identification of the item in the current line when the asynchronous loading is completed, it will be displayed. Otherwise, it will not be processed.
Implementation code in andbase:
The above content is the analysis and solution of dislocation, repetition and flicker of listview asynchronous loading pictures in Android. I hope it will be helpful to your work and study in the future.