The reason and the solution of the Listview of the image during the slide of Android _android

Source: Internet
Author: User
Tags repetition

The main analysis of Android ListView scrolling process caused by the image display repetition, confusion, flashing reasons and solutions, by the way to follow the ListView caching mechanism.

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. In the process of sliding,

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

b, to get the line item that slides into the screen, you first determine if there is an item available in the cache, and if so, pass it to the adapter GetView as the Convertview parameter.

In this case, the GetView can be used to make full use of the cache to greatly improve the performance of ListView. Even tens of thousands of line items, the maximum number of inflate is n,n for a screen to display the number of ListView line item.

@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 at the same time poses some problems.

A, line item picture shows duplicate

This display repetition refers to the current line item showing a picture of the item in the previous row

For example, ListView slide to the second line to load a picture asynchronously, but the load is very slow, loading process ListView has been loaded to 15 lines, and the slide process of the picture loading ended, the second row is no longer on the screen, according to the caching principle described above, the second row of view may be the 14th row So what we're looking at is 14 lines showing a picture that's supposed to be in the second row, causing repetition.

b, line item picture shows disorder

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 second line to load a picture asynchronously, but the load is slow, the load process ListView has slipped to 14 lines, the second row is no longer on the screen, according to the caching principle described above, the second row of view may be the 14th row of reuse, Line 14th shows the second row of view when the picture before the load ends, it will appear in line 14th, causing confusion.

C, line item artwork display flashing

The above B case, 14 lines of pictures and quickly loaded the end, so we see the 14th line first show the second row of pictures, and immediately showed their own pictures to cover the flicker caused by 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, in the asynchronous loading when the comparison between the identity and the current item identification is consistent, the same is shown, otherwise do not handle it.

Add the Code

@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 ();
Imageview.settag (ImageUrl);
if (!cache.icon_catch.get (imageurl,imageview) {
imageview.setimagedrawable (null);
}
}

Where the Settag represents the setting of the logo to facilitate the following flag comparison

if (! Cache. Icon_cache. Get (IMAGEURL, ImageView))

Cache.icon_cache is an instance of Imagecache, which means that if it is not in the cache, the setting drawable is null (you can set your own default resource, of course) to prevent the display of a picture of a previous line item, which resolves A. The row item picture shows a duplicate problem.

Add in the onimageloaded function of the Imagecache Onimagecallbacklistener

Java

public void onimageloaded (String imageUrl, drawable imagedrawable, view view, Booleanisincache) {
//CAN is Anot  Her view child, like TextView and so on
if (view!= null && imagedrawable!= null) {
ImageView ImageView = (ImageView) view;
Add tag judge, avoid ListView cache and so on
String Imageurltag = (string) ImageView. Gettag ();
if (objectutils. Isequals (Imageurltag, ImageUrl)) {
imageview. setimagedrawable (imagedrawable);
}
}
} ;

On the above, use string Imageurltag = (string) Imageview.gettag (), get the tag that was set before, and then compare it to the current URL, and if it is equal, resolve B. Row item picture shows disorder, C. Row item picture shows two problems of disorder. which objectutils visible objectutils@github.

Other asynchronous load process resolution principles are similar.

The above is a small set to introduce the Android Listview slide in the process of the clues to repeat the confusion of the reasons and solutions, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.