Android的 - 问题与延迟加载图像到一个ListView [英] Android - Issue with lazy loading images into a ListView

查看:107
本文介绍了Android的 - 问题与延迟加载图像到一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很常见的场景:在ListView它必须从互联网上下载的显示图像

This is a very common scenario: displaying images in a ListView which have to be downloaded from the internet.

现在我有ArrayAdapter,我使用的ListView控件的自定义子类。在我getView()实现一个ArrayAdapter,我产生一个单独的线程来加载图像。加载完成后,它查找适当的ImageView并将图像ImageView.setImageDrawable()。所以我用的解决方案是一种类似于此:<一href="http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview">http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview

Right now I have a custom subclass of ArrayAdapter which I use for the ListView. In my getView() implementation of the ArrayAdapter, I spawn a separate thread to load an image. After the loading is done, it looks up the appropriate ImageView and sets the image with ImageView.setImageDrawable(). So the solution I used is kind of similar to this one: http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview

我遇到的问题是,当我拨打电话,以在ImageView的setImageDrawable(),ListView中莫名其妙地刷新所有当前可见行列表!这导致一种无限循环:

The problem I'm having is that as soon as I make the call to setImageDrawable() on the ImageView, the ListView somehow refreshes all currently visible rows in the list! This results in kind of an infinite loop:

  1. 在getView()被调用
  2. 在线程催生加载图片
  3. 在图像加载; setImageDrawable()调用的ImageView
  4. 在ListView控件将它拾起出于某种原因,并刷新自己
  5. 对于ListView刷新,getView()被调用每个可见行,让我们回到步骤1和整件事重演

所以,就我所看到的,在提出的解决方案机器人 - 如何做的ListView图像的延迟加载(见以上链接)根本不起作用。它看起来像它,但它会运行得非常慢,因为在后台,它使重装的当前可见行。

So as far as I can see, the solution proposed in "Android - How do I do a lazy load of images in ListView" (see link above) simply doesn't work. It might look like it does, but it will run very slow because in the background, it keeps reloading the currently visible rows.

有没有人之前会遇到这样的和/或有一个解决方案?

Did anyone encounter this before and/or have a solution for this?

推荐答案

在链接的解决方案, fetchDrawableOnThread()只应调用,如果认为已经没有了正确的绘制。

In the linked solution, fetchDrawableOnThread() should only be called if the view does not already have the correct drawable.

一个视图没有被拉伸,如果 getDrawable()返回null。

A view does not have a drawable if getDrawable() returns null.

如果您正在重用的插槽,你认为你需要进一步的去管理国家。如果你的观点有一个成员变量存储网址和一个布尔说是否加载它,它会很容易知道是否调用 fetchDrawableOnThread()与否,例如,

If you are reusing slots, you views you need to go further and manage the state. If your views have a member variable storing the URL, and a boolean to say whether it is loaded, it'd be easy to know whether to call fetchDrawableOnThread() or not, for example.

我推测,绘制的的toString()详细从中加载图片的路径。 (如果没有,你可以在子类返回绘制做出这等)。在这种情况下,可以避免上面概述的布尔和只是一个比较,以确定其是否正确可拉伸或是否获取替换

I'd speculate that the drawable's toString() detailed the path from which the image was loaded. (If it doesn't, you could subclass the drawable returned to make it so). In this case, you could avoid the boolean outlined above and just do a comparison to determine if its the right drawable or whether to fetch a replacement.

此外,您getView()上可见行应确保那些不再可见获取卸载的,以prevent内存耗尽。一个技巧是将移动不再可见图像软引用(因此它们被卸载时需要内存)作为另一个海报上的原帖说。

Additionally, your getView() on a visible row should ensure that those that no longer visible get unloaded, to prevent memory exhaustion. A finesse would be to move the no longer visible images to soft references (so they are unloaded when memory is needed) as another poster on the original thread noted.

这篇关于Android的 - 问题与延迟加载图像到一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆