【问题标题】:Sometimes Picasso doesn't load the image from memory cache有时毕加索不会从内存缓存中加载图像
【发布时间】:2014-05-19 23:39:53
【问题描述】:
 private class CustomAdapter extends CursorAdapter {

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if (view != null) {

        String url = cursor.getString(CONTENT_URL_COLUMN);
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        final ImageView imageView = viewHolder.mImageViewIcon;
        final TextView textView = viewHolder.mTextViewName;

            Picasso.with(context).load(url).into(new Target() {

                @Override
                public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
                    imageView.setImageBitmap(arg0);
                    imageView.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.GONE);
                }

                @Override
                public void onBitmapFailed(Drawable arg0) {
                    imageView.setVisibility(View.GONE);
                    textView.setVisibility(View.VISIBLE);
                }
             });
        }
    }
}
}

如果图像列表已经下载,那么在快速滚动列表时, onBitmapLoaded() 方法调用并从内存缓存中加载图像。但有时会调用 onBitmapFailed()。为什么?

【问题讨论】:

标签: android picasso


【解决方案1】:

您的Target 正在被垃圾收集,因为没有任何东西持有对它的引用。毕加索在持有ImageViews 或Targets 时使用WeakReference

但是,您根本不需要使用Target。只需使用.into的回调,直接传递ImageView即可。

Picasso.with(context).load(url).into(imageView, new Callback() {
  @Override public void onSuccess() {
    imageView.setVisibility(VISIBLE);
    textView.setVisibility(GONE);
  }

  @Override public void onError() {
    imageView.setVisibility(GONE);
    textView.setVisibility(VISIBLE);
  }
});

【讨论】:

  • Target 是必要的,如果你想像 LinearLayout 一样设置 GroupView 的背景。
猜你喜欢
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
  • 2015-05-07
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 2015-07-17
相关资源
最近更新 更多