懒加载列表是什么?

21

我找不到任何可信的来源来解释LazyList是什么。有人知道吗?

4个回答

39

懒加载列表是从SD卡或服务器使用URL懒加载图像。它就像按需加载图像一样。

图像可以缓存到本地SD卡或手机内存中。 URL被视为键。如果键存在于SD卡中,则从SD卡显示图像,否则它会从服务器下载图像并将其缓存到您选择的位置。您可以设置缓存限制。您也可以选择自己的位置来缓存图像。缓存也可以清除。

懒加载列表在用户等待下载大型图像并显示它们的过程中,可以按需加载图像。由于图像被缓存,您可以离线显示图像。

https://github.com/thest1/LazyList. 懒加载列表。

在您的getview中

imageLoader.DisplayImage(imageurl, imageview);

ImageLoader显示方法

    public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters
    {
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);   //get image from cache using url as key
    if(bitmap!=null)         //if image exists
        imageView.setImageBitmap(bitmap);  //display iamge
     else   //downlaod image and dispaly. add to cache.
     {
        queuePhoto(url, imageView);
        imageView.setImageResource(stub_id);
     }
   }

替代Lazy List的方案是Universal Image Loader

https://github.com/nostra13/Android-Universal-Image-Loader。它基于Lazy List(使用相同的原理),但具有许多其他配置选项。我更喜欢使用Universal Image Loader,因为它提供了更多的配置选项。它可以在下载失败时显示错误图像。 它可以显示带圆角的图像。它可以在磁盘或内存中缓存。它可以压缩图像。

在您的自定义适配器构造函数中

  File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
 // Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
          // You can pass your own memory cache implementation
         .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
         .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
         .enableLogging()
         .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();

在你的getView()方法中

  ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
  imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options

您可以使用其他选项配置Universal Image Loader以满足您的需求。

除了LazyList / Universal Image Loader之外,您还可以查看此网站,以实现平滑滚动和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html.


嘿,Raghu,我无法从本地URI获取图像,请帮忙吗??http://stackoverflow.com/questions/20109034/lazy-list-fetching-images-from-sdcard-local-url - Ankit Srivastava
很好的解释,那么Lazy List类似于Picasso或Glide吗? - Fshamri
1
@user2198400,这些是图像缓存库。是的,它们有助于实现懒加载。此外,还要了解一下ListView的回收机制。 - Raghunandan
感谢您的快速回复,有没有使用Picasso或Glide与lazy list的示例?提前致谢。 - Fshamri
@user2198400 请在GitHub上搜索Chris Banes的chessemaster。 - Raghunandan

9
据我所知,我将用一个例子来解释给您听。如果您的列表包含很多带有文本的图片,那么加载列表需要一些时间,因为您需要下载这些图片并将它们填充到列表中。假设您的列表包含100张图片,每个图片都需要下载和显示在列表项中,这将需要很长时间。让用户等待图片加载完成不是很友好的体验。因此,我们需要采取什么措施呢?这时候懒加载就出现了。它的思想是让图片在后台加载,同时先显示文本。
众所周知,ListView对于每个视图都进行回收。例如,如果您的ListView包含40个元素,则ListView不会为这40个项目分配内存,而是只为可见的10个项目分配内存。所以,ListView只会分配10个项目的内存。
因此,每当您滚动视图时,视图就会刷新。由于这样,您将失去对图片的引用,需要重新下载。为了避免这种情况,缓存应运而生。
这个例子是基于我的ListView知识,我并不是说这是唯一正确的。答案可能存在错误,如果有人发现,请随时告知我。

5
我认为这个说反了。据我所知,惰性加载才是正确的定义,即只有在需要时才加载数据,这是一种良好的设计实践。
因此,我认为对于列表视图也适用相同的方法。
如果我错了,请纠正我。

2
最好的“懒加载”示例是Facebook的通知、消息和请求。当您滚动时,数据将被加载。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接