13

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

6 回答 6

5

嗨,丽莎安妮,请试试这个

ViewHolder h; //declare viewholder global

您使用查看器获取视图

@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(),这会降低性能。即使 Adapter返回一个用于回收的膨胀视图,您仍然需要查找元素并更新它们。重复使用 findViewById() 的一种方法是使用“视图持有者”设计模式。

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

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

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

于 2015-07-21T05:22:52.313 回答
1

正如我在此链接中发现的那样:毕加索问题 回收视图时,您应该在加载新请求之前调用 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;
}
于 2015-07-23T07:53:11.660 回答
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;
    }
 }

你应该没事。

于 2015-07-20T13:20:42.793 回答
0

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

将 parentView 传递给您的 convertView。

于 2015-07-22T11:01:09.307 回答
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;
于 2015-07-23T05:28:48.583 回答
-1

尝试这个

convertView = inflater.inflate(R.layout.square, parent, false);
于 2015-07-23T11:43:58.070 回答