【问题标题】:Is it absolutely correct that Picasso understands NOT to load if the view has been recycled?如果视图已被回收,毕加索理解不加载是否绝对正确?
【发布时间】:2014-09-09 13:48:31
【问题描述】:

我有点困惑:通常,当将图像异步加载到某种列表视图(无论是在 Android 或 iOS 上还是在另一个平台上抽象地)时,您基本上必须这样做..

-- make a note of "which" cell this is (say, #213)
-- start getting the image from the net.
-- it has loaded from the net. What cell are we now?
-- if we are "still" 213, load the image to the image view!
-- if we are "no longer" 213, just forget about it.

这是延迟加载异步图像的基础。例如,Lucas Rocha 在这里的一篇著名文章中完美地解释了这一点:

http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

(向下滚动到确切的“这只是您可以做到的一种方法的简单草图:” ...)

现在好了,据我了解实际上,毕加索完全自动地为您做到了这一点。

毕加索自己“知道”视图是否发生了变化。如果视图发生了变化,毕加索知道不必费心加载它

我完全正确吗?这是毕加索的内置功能,我不需要做任何其他事情吗?

(除此之外 - 我有点困惑“毕加索是如何做到这一点的;瞥一眼它,我看不到毕加索的任何魔术代码,它记录了 id 或持有者的某些东西?视图?有问题.)


为了清楚起见,我以通常的方式使用 Picasso,基本上是在 getView 的末尾...

Picasso.
  with(State.mainContext).
  load(imageFile.getUrl()).
  placeholder(R.drawable.default).
  noFade().
  into(v.hexaIV);

【问题讨论】:

  • 假设您正在使用 Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 从适配器的 getView 加载图像...加载到尚未完成但取消上一个下载的同一 imageView(在视图被重用后)... Piccaso对象有 2 个地图来存储 ImageView(或其他目标)上发生的操作 ... targetToAction 和 targetToDeferredRequestCreator

标签: android lazy-loading picasso async-loading


【解决方案1】:

是的,Picasso 就是这么漂亮。你只需要在 getView() 方法中的那一行 Picasso.with(c).load(url).into(img);

他们到底是怎么做的,我不确定,但在 Picasso 存在之前,我做了自己的图像加载器,这并不难。

假设您在图像加载器代码中的某处有一个 Url 和 ImageView 的地图。

因此,每当代码将img 传递给它时,它都会检查此映射,是否已经使用基本Java mImg.equals(img) 为同一img 加载其他URL,如果匹配,它知道,即使你仍然会缓存该 URL,它不应该将 Drawable 传递给 ImageView。

在极少数情况下,您可能希望直接取消加载,在这些情况下,您可以致电 Picasso.with(c).cancel(img);,但这很少有必要。

【讨论】:

  • 感谢一百万的精彩回答,@Budius! (对不起,我离开了,直到现在才“打勾”。)顺便说一句,这是我的许多 Android 专家朋友的一个挑战问题......stackoverflow.com/questions/25747696/…
猜你喜欢
  • 2018-10-03
  • 2017-06-15
  • 2017-10-30
  • 2014-08-29
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
相关资源
最近更新 更多