Android ListView Slide Process picture Display repeat misplaced flashing problem solved [reprint]

Source: Internet
Author: User
Tags repetition

Turn from: here

The main analysis of the Android ListView scrolling process in the picture shows the repetition, confusion, flashing reasons and solutions, incidentally mention the cache mechanism of the ListView.
1. Cause analysis
ListView Item Caching mechanism : to make the performance better, the ListView caches the row item (the view for a row). The ListView obtains the item for each line through the GetView function of the adapter. During the sliding process,

A. If a line item has been slid out of the screen, if the item is not in the cache, put in the cache, otherwise update the cache;
B. Gets the line item that slides into the screen before it determines if there is an item available in the cache, and if so, the getview that is passed to adapter for the Convertview parameter.
More specifically visible source code Listview.obtainview.

This way, the GetView can be used to make full use of the cache to greatly improve the performance of the ListView. Even if tens of thousands of rows item, the maximum number of inflate is n,n for a screen to display the number of ListView row item.

1 @Override2  PublicView GetView (intposition, View Convertview, ViewGroup parent) {3 Viewholder Holder;4     if(Convertview = =NULL) {5Convertview = Inflater.inflate (R.layout.list_item,NULL);6Holder =NewViewholder ();7 ...8 Convertview.settag (holder);9}Else {TenHolder =(Viewholder) Convertview.gettag (); One     } A } -  - /** the * Viewholder -  *  -  * @author[email protected] 2013-08-01 -  */ + Private Static classViewholder { -  + ImageView AppIcon; A TextView AppName; at TextView AppInfo; -}

This improves performance, but it also creates additional problems:

A. The line item picture displays duplicates
This display repetition means that the current line item shows a picture of the previous line item.
For example, the ListView slides to the 2nd line to load a picture asynchronously, but the load is slow, the ListView has slid to the 14th row during the loading process, and the picture is loaded at the end of the slide, line 2nd is no longer on the screen, according to the above-mentioned caching principle, the 2nd line view may be re-used by the 14th line , so what we see is that line 14th shows the image that should be on line 2nd, causing the display to repeat.

B. Line item picture shows confusion
This display disorder refers to a line item that shows a picture that does not belong to the line item.
For example, the ListView slides to the 2nd line to load a picture asynchronously, but the load is slow, the ListView has been sliding to the 14th row, the 2nd line is no longer on the screen, according to the caching principle described above, the 2nd line of view may be reused by the 14th line, Line 14th shows the view of line 2nd, when the previous picture is loaded, it will appear in line 14th, causing confusion.

C. Line item picture Display flashes
In the case of the above B, the 14th line of the picture again quickly loaded the end, so we see the 14th line first shows the image of the 2nd line, and immediately show their own image to cover the flicker caused by confusion.

2. Solution
Through the above analysis we know that the cause of the disorder is the asynchronous loading and the object is reused, if each time GetView can give an identity to the object, when the asynchronous loading is done to compare the identity with the current row of the identity of the item is consistent, the same is displayed, otherwise do not do processing.
The following is an example of using Imagecache to provide a picture capture cache for a ListView, and Imagecache is strongly recommended in the ListView.
First, add the GetView in the ListView adapter

1 @Override2  PublicView GetView (intposition, View Convertview, ViewGroup parent) {3 Viewholder Holder;4     if(Convertview = =NULL) {5Convertview = Inflater.inflate (R.layout.list_item,NULL);6Holder =NewViewholder ();7 ...8 Convertview.settag (holder);9}Else {TenHolder =(Viewholder) Convertview.gettag (); One     } A  - ... -     //add tag for image, to compare it when image loaded finish the Imageview.settag (IMAGEURL); -     //if not in cache, restore default -     if(!Cache.ICON_CACHE.get (IMAGEURL, ImageView)) { -Imageview.setimagedrawable (NULL); +     } -}

Where Settag indicates the setting of the identity, convenient for the following flag comparison

1 if (! Cache.ICON_CACHE.get (IMAGEURL, ImageView))

Cache.icon_cache is an instance of Imagecache, which means that if it is not inside the cache, setting drawable is null (you can, of course, set it to your own default resource), preventing a picture of the previous line item from being displayed, resolved a. The line item picture shows a repeating problem.

Added in the Ongetsuccess function of the Imagecache Onimagecallbacklistener

1  Public voidOngetsuccess (String imageUrl, drawable imagedrawable, view view,BooleanIsincache) {2     //can be another view child, like TextView and so on3     if(View! =NULL&& imagedrawable! =NULL) {4ImageView ImageView =(ImageView) view;5         //add tag judge, avoid ListView cache and so on6String Imageurltag =(String) Imageview.gettag ();7         if(Objectutils.isequals (Imageurltag, ImageUrl)) {8 imageview.setimagedrawable (imagedrawable);9         }Ten     } One};

Use string Imageurltag = (string) Imageview.gettag () above, get the tag that was set earlier, compare with the current URL, and if equal, resolve B. Line item picture shows confusion, C. Line item picture shows two problems with confusion. where objectutils visible [email protected].

Other asynchronous loading procedures work similarly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.