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

时间:2015-07-14 13:04:59

标签: android picasso convertview

这是我的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;
}

6 个答案:

答案 0 :(得分:5)

Hi Lisa Anne请试试这个

ViewHolder h; //declare viewholder global

您使用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;
}

观看者类

static class ViewHolder {
     ImageView  image;
  }

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

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

视图支架是非常有用的技术,尤其是在显示图像时。 Android Developer documentation

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

答案 1 :(得分: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();
}

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

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;
    }
 }

你应该没事。

答案 2 :(得分:1)

正如我在此链接中找到的那样:picasso issue 回收视图时,应在加载新请求之前调用cancelRequest。所以代码应该是:

@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);
    Picasso.with(context).cancelRequest(image); // cancel old request
    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;
}

答案 3 :(得分:0)

convertView = inflater.inflate(R.layout.square,parent,false);

将parentView传递给convertView。

答案 4 :(得分:0)

    try this one:
    It is working smoothly for me:

    @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.row_events, null);
                holder = new ViewHolder();
                holder.txtdName = (TextView) convertView
                        .findViewById(R.id.txtTitle);
                holder.txtdate = (TextView) convertView.findViewById(R.id.txtDate);

                holder.txtSubTitle = (TextView) convertView
                        .findViewById(R.id.txtSubtitle);
                holder.imgEvent = (ImageView) convertView
                        .findViewById(R.id.imgEvents);
                holder.imgArrow = (ImageView) convertView
                        .findViewById(R.id.imgArrow);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            try {
                holder.txtdName.setText(listd.get(position).getName());
                holder.txtdate.setText(Constants.dateFormat(listd.get(position)
                        .getStart_date()));
                holder.txtSubTitle.setText(listd.get(position).getDescription());
                holder.txtSubTitle.setTextColor(Color.GRAY);
                String imgUrl = listd.get(position).getImageUrl();
                // imageLoader.displayImage(imgUrl, holder.imgEvent, options);
                Picasso.with(context).load(imgUrl).into(holder.imgEvent);

return convertView;

答案 5 :(得分:-1)

试试这个

convertView = inflater.inflate(R.layout.square, parent, false);