android - picasso + convertView : what am I doing wrong here?

标签 android picasso convertview

这是我的getView()

我显然在这里做错了什么,因为我列表的第一项总是没有显示图片。

这里的问题出在 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;
}

最佳答案

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

ViewHolder h; //declare viewholder global

你用 viewholder 获取 View

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

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

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

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

关于android - picasso + convertView : what am I doing wrong here?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407476/

相关文章:

android - 监控 Android 系统设置值

android - 有时 Picasso 不会从内存缓存中加载图像

android - 为什么 Picasso Debug模式不起作用?

android - 如何使用字符串搜索将一个布局扩展到另一个布局

android - 在 android 列表适配器中,如果 convertView 为 null 而不是 null 怎么办?任何人都可以解释下面的 getView 方法

android - getgroupview 中的 convertview 无法正常工作

Android:第二次打开 fragment 时工具栏上的 "up"按钮消失

android - iOS 从 Google Drive Swift 流视频

android - 来自某些网址的图片未加载到 picasso 中

android - 在 Xamarin Forms Custom Renderer 中将 Mapbox 添加到 Android View