Picasso 将图片加载到列表适配器中的错误图像视图 [英] Picasso loads pictures to the wrong imageview in a list adapter

查看:18
本文介绍了Picasso 将图片加载到列表适配器中的错误图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 picasso 将图像从服务器加载到列表视图项,如下所示:

public View getView(int position, View convertView, ViewGroup parent) {LayoutInflater inflater = (LayoutInflater) 上下文.getSystemService(Context.LAYOUT_INFLATER_SERVICE);查看参与者视图;if(convertView == null) {参与者视图 = inflater.inflate(R.layout.participant_item, parent, false);} 别的 {参与者视图 = 转换视图;}TextView textView = (TextView)participantView.findViewById(R.id.participantName);textView.setText(getItem(position).getName());ImageView imageView = (ImageView)participantView.findViewById(R.id.participantImage);String profilePic = getItem(position).getProfilePic();if(!profilePic.equals("None")) {Log.d("tom.debug", "为用户创建图片:" + getItem(position).getName());Picasso.with(this.context).load(urlToProfilePics + profilePic).placeholder(R.drawable.sample_0).resize(52, 52).into(imageView);} 别的 {//将占位符加载到图像视图中Picasso.with(this.context).load(R.drawable.sample_0);}if(!getItem(position).isHere()) {imageView.setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);}返回参与者视图;}

if 语句下的调试日志仅针对真正拥有个人资料图片的用户触发.(没有的用户将获得 None 的值).

但是,某些其他列表视图项(没有个人资料图片)也会加载图片.

另一个有用的事实(我认为):向上和向下滚动列表时,出现错误的项目会发生变化.

我不确定我在这里遗漏了什么.

解决方案

确保每次您要在适配器的 getView() 上使用 Picasso 时调用 cancelRequest..

//1st: 重置 imageViewPicasso.with(this.context).cancelRequest(holder.imageView);//2nd 为 imageView 启动一个新的加载Picasso.with(this.context).load(...).into(holder.imageView);

原因是您从 convertView 参数重用的视图属于前一行,毕加索可能已经为另一张图片加载了该行.

这仅在使用 convertView 时才真正需要,如果您刚刚扩充了一个新布局,则不需要它..但您可以随时调用以使您的代码更容易.>

I'm loading an image from a server to a list view item using picasso like this:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View participantView;
    if(convertView == null) {
        participantView = inflater.inflate(R.layout.participant_item, parent, false);
    } else {
        participantView = convertView;
    }

    TextView textView = (TextView) participantView.findViewById(R.id.participantName);
    textView.setText(getItem(position).getName());
    ImageView imageView = (ImageView) participantView.findViewById(R.id.participantImage);
    String profilePic = getItem(position).getProfilePic();

    if(!profilePic.equals("None")) {
        Log.d("tom.debug", "creating picture for user: " + getItem(position).getName());
        Picasso.with(this.context)
            .load(urlToProfilePics + profilePic)
            .placeholder(R.drawable.sample_0)
            .resize(52, 52)
            .into(imageView);
    } else {
        //load the place holder into the image view
        Picasso.with(this.context).load(R.drawable.sample_0);
    }

    if(!getItem(position).isHere()) {
        imageView.setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
    }

    return participantView;
}

The debug log under the if statement only fires for users that really have a profile picture. (Users that don't have will get a value of None).

However, some of the other list view items (that don't have a profile pic) also get the picture loaded.

Another useful fact (I think): The items that get the bug changes when scrolling up and down the list.

I'm not sure what I'm missing here.

解决方案

Make sure you call the cancelRequest everytime you are about to use Picasso on a getView() from the Adapter..

// 1st: reset the imageView
Picasso.with(this.context).cancelRequest(holder.imageView); 

// 2nd start a new load for the imageView
Picasso.with(this.context).load(...).into(holder.imageView); 

The reason is that the view you are reusing from the convertView parameter belongs to a previous row that was possibly already being loaded by Picasso for another picture.

This is only really necessary when using the convertView, if you have just inflated a new layout, it won't be needed..but you can call always to make your code easier.

这篇关于Picasso 将图片加载到列表适配器中的错误图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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