[Android] Optimization of delayed image loading in the Adapter. getView () method of ListView

Source: Internet
Author: User

[Android] Optimization of delayed image loading in the Adapter. getView () method of ListView
For example, if you use the friend list as an example, each Item in ListView represents a friend. Each friend has an avatar, which needs to be loaded from the server to a local location and then displayed in the item. Obviously, the process of starting image loading should be triggered in the getView () method, start a thread, and then download the Avatar image. Here I am writing an open source framework ImageLoaderSample (https://github.com/wangjiegulu/ImageLoaderSample) to load images and implement memory cache and local cache. Amount -- here I will not introduce ImageLoaderSample usage, to a portal: http://www.cnblogs.com/tiantianbyconan/p/3574131.html and then look at the call time of the getView () method: 1. when the Adapter calls yydatachanged 2. when ListView is rolled, that is, convertView is continuously reused. That is to say, the getView () method is continuously called every time the ListView is rolled, and the image download process is continuously executed (of course, there will be cache in ImageLoaderSample, but the memory cache is limited, if the image to be displayed cannot be found in the memory cache, it needs to be searched in the File Cache for io read/write, which is relatively time-consuming). Obviously, there is room for optimization. How can we optimize it? As long as the ListView does not perform io read/write when the image is displayed during scrolling, the specific logic is as follows:-If the ListView is stopped when the GetView method is called, first, search for the Avatar image in the memory. If the memory image exists, the saved image in the memory is displayed. If the memory image does not exist, search for it in the File Cache. If the cached image exists, the cached image is displayed. If the cached image does not exist, download the image Online Based on the url and display it.-If the getView method is called, The ListView is in the scrolling state, find the picture of the Avatar in the memory. If the memory image exists, the saved image in the memory is displayed. If the memory image does not exist, then, a default image is displayed (saving the process of searching for images from the File Cache and requesting images from the network ). In this case, we must rewrite the BaseAdapter so that it can monitor the scrolling status of the ListView and obtain the scrolling status of the current ListView In the Adapter. So transform BaseAdapter, ABaseAdapter (https://github.com/wangjiegulu/AndroidBucket/blob/master/src/com/wangjie/androidbucket/adapter/ABaseAdapter.java): Copy code 1 package com. wangjie. androidbucket. adapter; 2 3 import android. widget. *; 4 import com. wangjie. androidbucket. adapter. listener. onAdapterScrollListener; 5 6/** 7 * Author: wangjie 8 * Email: tiantian.china.2@gmail.com 9 * Date: 12/3/14.10 */11 public AB Stract class ABaseAdapter extends BaseAdapter implements AbsListView. onScrollListener {12 private OnAdapterScrollListener onAdapterScrollListener; 13/** 14 * whether the current listview is in the rolling status 15 */16 private boolean isScrolling; 17 18 public boolean isScrolling () {19 return isScrolling; 20} 21 22 public void setOnAdapterScrollListener (OnAdapterScrollListener onAdapterScrollListener) {23 this. onAdapterScrollListener = OnAdapterScrollListener; 24} 25 26 protected ABaseAdapter (AbsListView listView) {27 listView. setOnScrollListener (this); 28} 29 30 @ Override31 public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {32 if (null! = OnAdapterScrollListener) {33 onAdapterScrollListener. onScroll (view, firstVisibleItem, visibleItemCount, totalItemCount); 34} 35} 36 37 @ Override38 public void onScrollStateChanged (AbsListView view, int scrollState) {39 if (null! = OnAdapterScrollListener) {40 onAdapterScrollListener. onScrollStateChanged (view, scrollState); 41} 42 43 // set whether to scroll status 44 if (scrollState = AbsListView. onScrollListener. SCROLL_STATE_IDLE) {// not rolling status 45 isScrolling = false; 46 this. notifyDataSetChanged (); 47} else {48 isScrolling = true; 49} 50} 51} copy the code, as shown in the preceding code. The Adapter implements AbsListView. onScrollListener, And the OnScrollListener is bound to the ListView In the constructor. The Changed method gets the current rolling status, saves the isScrolling status, and exposes the isScrolling () method to the outside. OnAdapterScrollListener this interface inherits AbsListView. onScrollListener, because OnScrollListener is set in one scene in the Adapter, it will be invalid if a new OnScrollListener is set outside, so you must provide another setOnAdapterScrollListener, then, input an OnScrollListener and perform callback in each method. This is because other interface methods may be extended in the future, therefore, a new interface is written here. (For future extension, the original code will not be affected. We recommend that you use OnAdapterScrollSampleListener to replace OnAdapterScrollListener, onAdapterScrollSampleListener class only implements NULL for all methods of OnAdapterScrollListener ). Then we write a MyAdapter that inherits ABaseAdapter, and then, in the getView () method, call the following method when you need to display the Avatar: // If You Are scrolling (search from memory, no network requests can be found.) ImageLoader. getInstances (). displayImage (headUrl, headIv, null, R. drawable. default_head, isScrolling (); can I see the wood?

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.