【问题标题】:What's LazyList?什么是懒惰列表?
【发布时间】:2013-03-15 08:31:21
【问题描述】:

我无法在任何真正可靠的来源中找到解释什么是 LazyList。有人吗?

【问题讨论】:

    标签: android lazylist


    【解决方案1】:

    Lazy List 是使用 url 从 sd 卡或服务器延迟加载图像。就像按需加载图片一样。

    图像可以缓存到本地 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);
         }
       }
    

    惰性列表的替代方法是通用图像加载器

    https://github.com/nostra13/Android-Universal-Image-Loader。它基于惰性列表(它的工作原理相同),但它有很多其他配置。我更喜欢使用 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 获取图像,你能帮忙吗??stackoverflow.com/questions/20109034/…
    • 很好的解释,所以懒惰列表类似于毕加索或滑翔?
    • @user2198400 这些是图像缓存库。是的,它们有助于延迟加载。另请阅读有关列表视图回收机制的信息。
    • 感谢您的快速回答,有没有使用 picasso 或 glide 的惰性列表的示例?提前致谢
    • @user2198400 从 Chris banes 在 github 中搜索 chessemaster
    【解决方案2】:

    AFAIK,我会用例子来解释你 如果您的列表包含大量带有文本的图像,则加载列表需要一些时间,因为您需要下载图像并且需要将它们填充到列表中。假设您的列表包含 100 张图片,下载每张图片并将其显示为列表项将花费大量时间。让用户等到图像加载对用户不友好。 所以我们需要做什么。此时,惰性列表出现了。这是让图像在后台加载并显示文本的想法。

    每个人都知道 listview 为每个视图回收它的视图。即如果您的列表视图包含 40 个元素,那么列表视图不会为 40 个项目分配内存,而是为可见项目分配内存,即您一次只能看到 10 个项目。所以 listview 将分配 10 个项目的内存。

    因此,当您滚动视图时,视图将刷新。因为您将失去对图像的参考,您需要重新下载它们。为了避免这种情况,缓存应运而生。

    这个例子是基于我在 listview 中的知识,我并不是说这只是正确的。答案可能有误,如果有人发现请随时通知我。

    【讨论】:

      【解决方案3】:

      惰性列表的最佳示例是 facebook 通知、消息、请求。当您滚动时,将加载数据。

      【讨论】:

        【解决方案4】:

        我认为这是相反的。 AFAIK,Lazy Loading 是定义,只有在需要时才实际加载数据,这是一个很好的设计实践。

        所以我相信这同样适用,只是这次它指的是列表视图。

        如果我错了,请纠正我。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-15
          • 2021-08-12
          • 2015-09-06
          • 1970-01-01
          • 2011-11-21
          • 1970-01-01
          • 2022-01-11
          • 1970-01-01
          相关资源
          最近更新 更多