毕加索错误在回收者视图中加载一些图像

时间:2017-09-07 13:37:08

标签: android caching picasso

我使用picasso在回收器视图适配器中加载一些图像,但有些图像显示加载成功但实际上没有显示

这是我的图片视图

   <ImageView
                android:id="@+id/news_main_imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                tools:src="@drawable/image" />

适配器代码

 if (holder instanceof NewsViewHolder) {


        ((NewsViewHolder) holder).newsTitleTextView.setText(currentArticle.getTitle().trim());
        ((NewsViewHolder) holder).newsDateTextView.setText(DateUtils.getDisplayDate(currentArticle.getPublishedAt(), context));

        if (!currentArticle.getThumbnail().isEmpty()) {
            ((NewsViewHolder) holder).higherLevelSpace.setVisibility(View.VISIBLE);
            ((NewsViewHolder) holder).newsTitleTextView.setGravity(Gravity.CENTER_VERTICAL);

            //Load the news image
            Picasso.with(context)
                    .load(currentArticle.getThumbnail().trim())
                    .networkPolicy(NetworkPolicy.OFFLINE)
                    .into(((NewsViewHolder) holder).newsMainImageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            ((NewsViewHolder) holder).newsImageScrim.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onError() {
                            ((NewsViewHolder) holder).newsImageScrim.setVisibility(View.GONE);

                            //Try again online if cache failed
                            Picasso.with(context)
                                    .load(currentArticle.getThumbnail().trim())
                                    .into(((NewsViewHolder) holder).newsMainImageView, new Callback() {
                                        @Override
                                        public void onSuccess() {
                                            ((NewsViewHolder) holder).newsImageScrim.setVisibility(View.VISIBLE);
                                        }

                                        @Override
                                        public void onError() {
                                            ((NewsViewHolder) holder).newsImageScrim.setVisibility(View.GONE);
                                            Timber.e("Could not fetch image");
                                        }
                                    });
                        }
                    });
        } else {
            ((NewsViewHolder) holder).higherLevelSpace.setVisibility(View.GONE);
            ((NewsViewHolder) holder).newsMainImageView.setVisibility(View.GONE);
            ((NewsViewHolder) holder).newsImageScrim.setVisibility(View.GONE);
        }

所以任何人都面临这个问题或者有解决方案

1 个答案:

答案 0 :(得分:0)

您可以使用Glide代替加载图片的最佳方式。

compile 'com.github.bumptech.glide:glide:3.7.0'


String imgUrl = "img.jpg";
ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
Glide.with(mContext).load(imgUrl)
            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView);