当我向上滚动列表时,我正在尝试重新使用已经下载的图像,但Picasso会再次从网络中获取所有图像。从图像中的红色标记可以看出这一点。
我使用此code作为参考,在自定义列表适配器中使用ViewHolder:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_widget_redeem, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.redeem_icon);
convertView.setTag(R.string.image_holder, holder);
} else {
holder = (ViewHolder) convertView.getTag(R.string.image_holder);
}
TextView titleView = (TextView) convertView.findViewById(R.id.redeem_title);
RedeemStrategy currentStrategy = getItem(position);
titleView.setText(currentStrategy.getName());
Picasso picasso = new Picasso.Builder(context).listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
exception.printStackTrace();
}
}).indicatorsEnabled(true).build();
picasso.load(AndroidApplication.context.getString(R.string.base_url) + currentStrategy.getLogo())
.tag(context)
.into(holder.image);
convertView.setTag(currentStrategy.getId());
return convertView;
}
@Override public RedeemStrategy getItem(int position) {
return redeemStrategies.get(position);
}
static class ViewHolder {
ImageView image;
}
如何让Picasso重新使用已下载的图像?
答案 0 :(得分:1)
您应该重复使用相同的Picasso实例,而不是每次都创建一个新实例。