gpt4 book ai didi

android - picasso 在使用 View 持有者模式时加载到错误的 ImageView 中

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:00 25 4
gpt4 key购买 nike

我正在尝试使用 Picasso 库将外部图像加载到 ListView 中的行中。我有一个自定义的 ArrayAdapter,如下所示:

public class RevisedBusinessesAdapter extends ArrayAdapter<HashMap<String, String>> {

Context context;
int layoutResourceId;
ArrayList<HashMap<String, String>> data = null;

public RevisedBusinessesAdapter(Context context, int layoutResourceId, ArrayList<HashMap<String, String>> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RevisedBusinessHolder holder = null;

if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RevisedBusinessHolder();
holder.ivLogo = (ImageView) row.findViewById(R.id.ivBusinessLogo);
row.setTag(holder);
} else {
holder = (RevisedBusinessHolder) row.getTag();
}

HashMap<String, String> business = data.get(position);

String strLogoURL = business.get("logoURL");
if (null != strLogoURL && !"".equals(strLogoURL)) {
Picasso.with(this.context).load(strLogoURL).into(holder.ivLogo);
}

return row;
}

static class RevisedBusinessHolder {
ImageView ivLogo;
}
}

其中 logoURL 是远程图像的 URL;如果未提供,ivBusinessLogo 有一个本地 src 集,而是显示。当我快速滚动时,Picasso 将图像加载到错误的 ImageView 中,我最终在列表中得到了它的多个副本。

this 的答案问题建议添加

Picasso.with(context).cancelRequest(holder.ivLogo);

在现有的 Picasso 调用之前,但这没有任何区别。如果我删除 row == null 检查并始终创建一个新 View ,它似乎工作正常。不过,在完整版中,还有四个 TextView 和五个其他图像(从本地资源加载的小图标,而不是通过 Picasso)需要在每个 getView 中更新。

有没有办法使它与 View Holder 模式一起工作 Android documentation推荐?

最佳答案

您应该始终调用 Picasso,即使您的 URL 为 null。这样它就知道 ImageView 被回收了。

删除这个if语句:

if (null != strLogoURL && !"".equals(strLogoURL)) {

您还应该考虑使用占位符图像或错误图像,以便在没有 URL 时显示某些内容。

如果您坚持保留 if 语句(但您不应该这样做!),您需要通过调用 cancelRequest 告诉 Picasso ImageView 已被回收:

Picasso.with(this.context).cancelRequest(holder.ivLogo);

关于android - picasso 在使用 View 持有者模式时加载到错误的 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26359608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com