Picasso + convertView:我在这里做错了什么?

Lis*_*nne 13 android convertview picasso

这是我的getView().

我显然在这里做错了,因为我列表中的第一项始终没有显示图片.

这里的问题是the convertview因为如果我不回收它,没有问题.

请问我做错了什么?

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null) //IF I DELETE THIS IF EVERYTHING OK!!!
    convertView = inflater.inflate(R.layout.square, null);
    ImageView image = (ImageView) convertView.findViewById(R.id.background_image);
    if (data.get(position).isFollowed()) {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(image);
    } else {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(image);

    }
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width / 2, width / 2);
    convertView.setLayoutParams(layoutParams);
    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

Cri*_*vez 5

嗨Lisa Anne请试试这个

ViewHolder h; //declare viewholder global
Run Code Online (Sandbox Code Playgroud)

您可以使用viewholder获取视图

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.square, parent, false);
            h = new ViewHolder();
            h.image = (ImageView) convertView.findViewById(R.id.background_image);
            convertView.setTag(h);
        }else{
            h = (ViewHolder)convertView.getTag();
        }
        //display in log and check if the the url of image is here and live.
        Log.e("checking","image file name"+data.get(position))
    if (data.get(position).isFollowed()) {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(h.image);
    } else {
        int approprieteimage = getApproppreiateImage(data.get(position));
        Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(h.image);

    }
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width / 2, width / 2);
    convertView.setLayoutParams(layoutParams);

    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

观察者类

static class ViewHolder {
     ImageView  image;
  }
Run Code Online (Sandbox Code Playgroud)

在滚动ListView期间,您的代码可能经常调用findViewById(),这会降低性能.即使适配器返回一个膨胀的视图以进行回收,您仍然需要查找元素并更新它们.重复使用findViewById()的方法是使用"视图持有者"设计模式.

ViewHolder对象将每个组件视图存储在Layout的标记字段中,因此您可以立即访问它们而无需重复查找它们.

View holder是非常有用的技术,尤其是在显示图像时.Android开发者文档

之后请告诉我你的日志错误.


Mar*_*rky 1

为什么不在适配器中使用 Holder 模式呢?

if (convertView == null) {
   convertView = inflater.inflate(R.layout.square, null, false);
   holder = new Holder(convertView);
   convertView.setTag(holder);
}
else {
   holder = (Holder) convertView.getTag();
}
Run Code Online (Sandbox Code Playgroud)

像这样创建你的持有者(用你的观点替换它)

public static class Holder {
    private View row;
    private TextView title;

    public Holder(View row) {
       this.row = row;
    }

    public TextView getTitle() {
       if (title == null) {
          title = (TextView) row.findViewById(R.id.title);
       }
       return title;
    }
 }
Run Code Online (Sandbox Code Playgroud)

你应该没问题。