Causes and solutions of repeated and disordered prompt pictures during Android listview sliding

This paper mainly analyzes the causes and solutions of picture display repetition, disorder and flicker caused by listview scrolling in Android, and follows up the cache mechanism of listview.

1. Cause analysis

Listview item caching mechanism: in order to achieve better performance, listview caches line items (the view corresponding to a line). Listview obtains the item of each line through the getview function of the adapter. During sliding,

a. If a line item has been drawn out of the screen, if the item is not in the cache, put it into the cache, otherwise update the cache;

b. Before obtaining the line item that slides into the screen, it will first determine whether there are available items 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 inflate is n, and N is the maximum number of listview line items displayed on one screen.

This improves performance, but it also causes some 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 the second line, a picture will be loaded asynchronously, but the loading is very slow. During the loading process, listview has been loaded to 15 lines, and the picture loading ends during the sliding process. The second line is no longer on the screen. According to the caching principle described above, the view of the second line may be taken by line 14, In this way, we see that 14 lines show the pictures that should belong to the second line, causing duplication.

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 listview slides to the second line, a picture will be loaded asynchronously, but the loading is very slow. During the loading process, listview has slid to line 14, and the second line is no longer on the screen. According to the caching principle described above, the view of the second line may be reused by line 14. Line 14 displays the view of the second line. At this time, after the previous picture loading is completed, it will be displayed on line 14, Cause chaos.

c. Line item image display flashes

In case B above, the loading of 14 lines of pictures is completed soon, so we see that the first line of 14 shows the pictures in the second line, and then shows the flicker disorder caused by their own pictures covering.

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, when the asynchronous loading is completed, compare whether the identification is consistent with the identification of the current item. If it is consistent, it will be displayed. If not, it will not be processed.

Add to the code

Settag means to set the flag, which is convenient for flag comparison below

Cache. ICON_ Cache is an instance of imagecache, which means that if it is not in the cache, set drawable to null (of course, you can set it as your own default resource), prevent the display of the picture of a previous line item, and solve the problem of duplicate display of A. line item pictures.

Add in onimageloaded function of onimagecallbacklistener of imagecache

Java

Use string imageurltag = (string) ImageView getTag(); Get the previously set tag, and then compare it with the current URL. If it is equal, it will be displayed. This solves the two problems of B. disordered display of line item pictures and C. disordered display of line item pictures. Where objectutils is visible ObjectUtils@Github .

The solution principle of other asynchronous loading processes is similar.

The above is what Xiaobian introduced to you about the causes and solutions of repeated and disordered pictures in the sliding process of Android listview. 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
分享
二维码
< <上一篇
下一篇>>