Picasso库未在列表视图中正确加载服务器中的图像

N S*_*rma 2 android image image-caching picasso android-recyclerview

我正在尝试使用RecylerView最近推出的Google.我有一组行,现在是7-8,每行都有一个我从服务器获取的图像.为此,我使用毕加索图书馆,但这对我不起作用.我不确定我是否遗漏了某些东西或配置了什么.

屏幕正确显示每行的默认图像,但不从服务器下载图像,如果服务器响应缓慢,我等待超过5分钟,但事实并非如此.

public DemoRecyclerAdapter(List<DemoRowModel> items, int itemLayout, final Context mContext) {
    this.items = items;
    this.itemLayout = itemLayout;
    this.mContext = mContext;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    DemoRowModel item = items.get(position);

    holder.mDemoNameTextView.setText(item.getDemoName());
    holder.mDemoDateTextView.setText(item.getDemoDate());

    Target mTarget = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            holder.mImageView.setImageBitmap(bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            Logger.d(TAG, "Failed! Bitmap could not downloaded.");
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {
        }
    };

    Picasso.Builder builder = new Picasso.Builder(mContext);
    Picasso picasso = builder.downloader(new OkHttpDownloader(mContext) {
        @Override
        protected HttpURLConnection openConnection(Uri uri) throws IOException {
            HttpURLConnection connection = super.openConnection(uri);
            // fetch the auth value
            SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext.getApplicationContext());
            connection.setRequestProperty(Constant.HEADER_X_API_KEY, mSharedPreferences.getString(SharedPreferenceKeys.JSESSIONID, ""));
            return connection;
        }
    }).build();

    picasso.load(item.getImagePath()).into(mTarget);

    // here set the value
    holder.itemView.setTag(item);

}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Vea*_*rji 5

如果您使用Target的是 - 它们应该是强引用对象.mTaget在类中创建字段并TargetonBindViewHolder方法移动初始化.

编辑:将安装密钥保存在安全的地方,比如keystore.不要保存它们SharedPreferences,这是一种不好的做法.

更新:

1)创建自定义Target类

public class CommonImageTarget implements Target {
    private final ImageView imageView;

    public CommonImageTarget(final ImageView imageView) {
        this.imageView = imageView;
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        this.imageView.setImageBitmap(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        Logger.d(TAG, "Failed! Bitmap could not downloaded.");
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
    }
}
Run Code Online (Sandbox Code Playgroud)

2)创建自定义 ImageView

public class ImageViewWithTarget extends ImageView{
    /**
     * Required for Bitmap loading using Picasso. Picasso uses weak references in into() method and Target's are garbage collected, save them in object.
     */
    private Target target;

    public ImageViewWithTarget(Context context) {
        super(context);
    }

    public ImageViewWithTarget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewWithTarget(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public Target getTarget() {
        return target;
    }

    public void setTarget(Target target) {
        this.target = target;
    }
}
Run Code Online (Sandbox Code Playgroud)

3)当你在初始化你viewHolder ImageView的,自定义设置Target在它

viewHolder.mImageView.setTarget(new CommonImageTarget(viewHolder.mImageView));
Run Code Online (Sandbox Code Playgroud)

4)更新ViewHolder课程

public class ViewHolder{
       ...
       private ImageViewWithTarget mImageView;
   }
Run Code Online (Sandbox Code Playgroud)

5)更换ImageViewImageViewWithTarget您的布局

6)更新onBindViewHolder方法

picasso.load(item.getImagePath()).into(viewHolder.mImageView.getTarget());
Run Code Online (Sandbox Code Playgroud)

现在每个人ImageView都会拥有自己的Target对象,而Target不是垃圾收集.

另一种方式:持有Target对象ViewHolder.