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