Android ListView Slide Process Picture Display Repeat confusion flicker problem solved

Source: Internet
Author: User

Transferred from: http://www.oschina.net/question/221817_121051

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.

ListView Adapter GetView Syntax Java
123456789101112 @Overridepublic  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 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

Java
1234567891011121314151617181920 @Overridepublic  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  ( )  ;     }      ......     //add tag for image, to compare it when image loaded Finis h     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 onimageloaded function of the Imagecache Onimagecallbacklistener

Java
1234567891011 public void onimageloaded  ( string imageurl , drawable imagedrawable  , View view , booleanisInCache )  {     //can be Another 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 )  ;         }     }} ;

Android listview During sliding process picture display Repeat confusion flashing problem resolution

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.