简体   繁体   中英

Lazy loading in android

I want to lazy load thumbnails of images that i receive via bluetooth in to a ListView . I saw many implementation of LazyLoading in android, but i just could not understand the flow how the lazy loader was working.

Btw I referred to this site

http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

In the above implementation they use both Memory cache as well as File cache (In the class ImageLoader). I can't get why is it necessary to maintain two cache?
And in the same class he maintains a Map imageViews. Why would he need a mapping of imageViews and Image url? .

Can anyone explain the flow of the above code or give some tips on designing my own lazyloader class.

Thanks in advance

I can't get why is it necessary to maintain two cache?

It is implementing a 2 level cache, so you have a first level cache in memory. This cache is faster that a disk cache(file cache) but it cannot have too much element on it. The second level cache, file cache, is a bigger one but is slower. So when it wants a image, first try to fetch it from the first level cache, if it cannot find it there, try on the second level cache. If the image is not in either, then download from a url. Once the image has been retrieved from the internet, is stored on the first level cache. If there are more image on the cache than the cache size, the least recent used image is moved from the first level cache to the second level.

Using a 2 level cache minimized the use of internet connection, storing those image that you have recently used.

Why would he need a mapping of imageViews and Image url?

The way the cache knows if the image you are requesting is already on the cache is looking in the map for the URL. Think on it as a ID of the image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM