Analysis and solution of dislocation, repetition and flicker of listview asynchronous loading images in Android _android

Source: Internet
Author: User
Tags repetition

Android ListView Asynchronous loading Image dislocation, repetition, flicker analysis and solutions, specific problem analysis and solutions please see below.

When we use ListView to load pictures asynchronously, in the case of fast sliding or bad network, there will be pictures dislocation, repetition, flicker and other problems, in fact, these problems summed up is a problem, we need to listview the optimization of these problems.

For example, there are 100 item on the ListView, a screen shows only 10 item, we know that GetView () Convertview is used to reuse the view object, because one item corresponds to a View object, The ImageView control is the view object obtained by Findviewbyid (), and we reuse the view object while the ImageView object is reused. For example, the 11th Item view reuse the 1th Item View object, then ImageView is also reused, so when the picture is not downloaded, this imageview (11th item) shows the data is the reuse (1th item) data.

1:item pictures show Duplicates

This display repetition refers to the current line item showing a picture of the item on the previous line.

For example, ListView slides to the 2nd line to load a picture asynchronously, but the load is slow, the ListView has slipped to the 14th row during the load, and the picture loads at the end of the slide. Line 2nd is no longer on the screen, according to the caching principle described above, the View object in line 2nd may be reused by the 14th line, so that we see that the 14th line shows the picture that should belong to the 2nd line, causing the display to duplicate.

2. Item picture shows disorder

This display disorder refers to a line item that shows a picture that does not belong to the line item.

The same reason as above.

3. Item picture Display flashing

In the other case described above, if the 14th line of the picture is loaded again soon, we see that line 14th shows the duplicate picture of the 2nd line, and immediately shows the image to cover it and cause flicker and confusion.

Solution:

Through the above analysis we know that the reason for the disorder is asynchronous loading and the object is reused, if each getview can give the object an identity, the asynchronous load when the comparison between the identity and the current line item identification is consistent, the consistent display, otherwise do not handle it.

principle : First give ImageView set a tag, this tag is set in the picture URL, and then in the loading time to get this URL and to load that position in the URL contrast, if not the same load, the same is reused before the load.

Android Displays pictures in ListView (repetitive confusion flashing problem)

1. Reason Analysis

ListView Item Caching mechanism:

To make performance better, ListView caches line item (view for a row).

ListView gets the item for each line through the adapter GetView function.

During the sliding process

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

B. Get the line item that slides into the screen before you determine if there is an item available in the cache, and if so, pass the Convertview parameter to adapter's GetView.

In this way, the following getview can be used to greatly improve the performance of ListView. Even tens of thousands of line item, the maximum number of inflate is N,

n Displays the maximum number of ListView line items for a screen.

@Override public
View getview (int position, View Convertview, ViewGroup parent) {
  Viewholder holder;
  if (Convertview = = null) {
   Convertview = inflater. Inflate (R. layout. List_item, null);
   Holder = new Viewholder ();
   ..... Convertview. Settag (holder);
  } else {
   holder = (viewholder) convertview. Gettag ();
  }
}

This improves performance, but it can also cause other problems:

A. Line item pictures show duplicates

This display repetition refers to the current line item showing a picture of the item on the previous line.

For example, ListView slides to the 2nd line to load a picture asynchronously, but the load is slow, the ListView has slipped to the 14th row during the load, and the picture loads at the end of the slide.

Line 2nd is no longer on the screen, according to the caching principle described above, view on line 2nd may be reused by the 14th line, so we see that the 14th line shows the picture that should belong to the 2nd line.

Cause the display to duplicate.

B. Line item picture showing confusion

This display disorder refers to a line item that shows a picture that does not belong to the line item.

For example, ListView slide to the 2nd line to load a picture asynchronously, but the load is slow, ListView has slipped to the 14th line in the load process, the 2nd row is not in the screen, according to the caching principle described above, the view of the 2nd line may be reused by the 14th line. Line 14th shows the view of line 2nd, at which point the previous picture loading ended, showing up in line 14th, causing confusion.

C. line item picture Display flashing

In the case of B above, the 14th line of the picture is quickly loaded and finished, so we see the 14th line shows the picture of the 2nd line, and immediately showed their own image to cover caused flicker confusion.

2. Solution method

Through the above analysis we know that the reason for the disorder is asynchronous loading and the object is reused, if each getview can give the object an identity, the asynchronous load when the comparison between the identity and the current line item identification is consistent, the consistent display, otherwise do not handle it.

Implementation code in Andbase:

/** * Displays this image to solve the list problem. * List problem: In the process of sliding, GetView ImageView will be reused, resulting in a string of images * @param imageview appears view * @param URL * @return/public void display (final imageview imageview,string URL) {if (Abstrutil.isempty (URL)) {if (noimage!= null) {if (
      Loadingview!= null) {loadingview.setvisibility (view.invisible);
     Imageview.setvisibility (view.visible);
    } imageview.setimagedrawable (Noimage);
   } return;
   //Set Download final Abimagedownloaditem item = new Abimagedownloaditem ();
   Set the size of the display item.width = width;
   Item.height = height;
   Set to zoom Item.type = type;
   Item.imageurl = URL;
   Final String CacheKey = Abimagecache. Getcachekey (Item.imageurl, Item.width, Item.height, Item.type);
   Item.bitmap = Abimagecache.getbitmapfromcache (CacheKey);
   if (D) log.d (TAG, "+cachekey+" obtained in the cache: "+item.bitmap);"
   Set tag Imageview.settag (URL); if (Item.bitmap = = null) {//show first in load if (loadingview!= null) {LOADINGVIew.setvisibility (view.visible);
    Imageview.setvisibility (view.invisible);
    else if (loadingimage!= null) {imageview.setimagedrawable (loadingimage); The Update interface Item.setlistener (new Abimagedownloadlistener () {@Override public void update (Bitmap bitm) after the download completes AP, String ImageUrl) {//The picture in load is not set, and a hidden view if (Loadingview!= null && imageurl.equals (IMAGEVIEW.G
       Ettag ())) {loadingview.setvisibility (view.invisible);
      Imageview.setvisibility (view.visible); //To determine if the URL of this imageview has changed, if there is no change to set,//change is canceled, solve the list of reuse view problems if (bitmap!=null&& imageurl.equal
       S (Imageview.gettag ()) {if (D) log.d (TAG, "Picture download, set:" +imageurl);
      Imageview.setimagebitmap (bitmap); else {if (errorimage!= null && imageurl.equals (Imageview.gettag ())) {imageview.setimagedrawable
       (Errorimage);
    }
      }
     }
    });
    if (D) log.d (TAG, "Picture Download, execute:" +url); Mabimagedownloadpool.Execute (item);
     } else {if (Loadingview!= null) {loadingview.setvisibility (view.invisible);
    Imageview.setvisibility (view.visible);
   } imageview.setimagebitmap (Item.bitmap); }
  }

The above content is the Android ListView asynchronous loading Picture dislocation, repetition, flicker problem analysis and solution, hope for everyone in the future work and learning help.

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.