Glide和RecyclerView - 图像搞砸了

时间:2017-12-14 16:29:54

标签: android android-recyclerview android-glide

我正在尝试使用ImageViews创建RecyclerView。这是我得到的:

enter image description here

正如你所看到的,所有的艺术家封面都搞砸了并且顺序错误。如果我滚动或重新加载,相应的图像可能会改变。

这是我的适配器onBindViewHolder:

public class ArtistsAdapter extends RecyclerView.Adapter<ArtistViewHolder> implements ListPreloader.PreloadModelProvider {
    private static final String TAG = ArtistsAdapter.class.getSimpleName();
    private final LayoutInflater mInflater;
    private final GlideRequest<Bitmap> fullRequest;

    ArtistsAdapter(Context context) {
        this.mContext = context;
        this.artists = new ArrayList<>();
        this.mInflater = LayoutInflater.from(context);
        this.fullRequest = GlideApp.with(context).asBitmap();
    }

    private final Context mContext;

    public List<CompactdArtist> getArtists() {
        return artists;
    }

    void swapArtists (List<CompactdArtist> artists) {
        this.artists.clear();
        this.artists.addAll(artists);
        notifyDataSetChanged();
    }

    private final List<CompactdArtist> artists;

    @Override
    public ArtistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_artist_item, parent, false);
        return new ArtistViewHolder(view);
    }

    @Override
    public void onViewRecycled(ArtistViewHolder holder) {
        super.onViewRecycled(holder);
        Log.d(TAG, "onViewRecycled: ");
        GlideApp.with(mContext).clear(holder.artistImageView);
        holder.artistImageView.setImageDrawable(null);
    }

    @Override
    public void onBindViewHolder(final ArtistViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: " + position);
        final CompactdArtist current = artists.get(position);

        // holder.setIsRecyclable(false);

        holder.artistImageView.setImageDrawable(null);
        try {
            fullRequest.load(new MediaCover(current))
                .fallback(ImageUtils.getFallback(mContext))
                .into(new ImageViewTarget<Bitmap>(holder.artistImageView) {
                    @Override
                    protected void setResource(@Nullable Bitmap resource) {
                        if (resource == null) {
                            // holder.setIsRecyclable(true);
                            return;
                        }

                        holder.artistImageView.setImageBitmap(resource);
                        int color = Palette.from(resource).generate().getMutedColor(0xFFFFFF);
                        try {
                            current.fetch();
                        } catch (CouchbaseLiteException e) {
                            e.printStackTrace();
                            holder.artistNameText.setText(R.string.unknown_artist_name);
                        }
                        holder.artistBackground.setBackgroundColor(color);
                        holder.bindArtist(current);
                        holder.artistSub.setText(current.getAlbumCount() + " albums");
                        holder.artistNameText.setText(current.getName());

                        // holder.setIsRecyclable(true);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        super.onLoadCleared(placeholder);
                        // holder.setIsRecyclable(true);
                    }
                });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public long getItemId(int position) {
        return artists.get(position).getId().hashCode();
    }

    @Override
    public int getItemCount() {
        return this.artists.size();
    }

    @NonNull
    @Override
    public List getPreloadItems(int position) {
        return this.artists.subList(position, position + 1);
    }

    @Nullable
    @Override
    public RequestBuilder<?> getPreloadRequestBuilder(Object item) {
        MediaCover cover = (MediaCover) item;
        return fullRequest.clone().thumbnail(0.2f).load(cover);
    }
}

以下是我的片段容器中的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_artists, container, false);

    mArtistRecyclerView = rootView.findViewById(R.id.artists_recyclerview);

    LinearLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
    mArtistRecyclerView.setLayoutManager(layoutManager);

    mArtistsAdapter = new ArtistsAdapter(getActivity());
    mArtistRecyclerView.setAdapter(mArtistsAdapter);
    mArtistRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
        @Override
        public void onViewRecycled(RecyclerView.ViewHolder holder) {
            ArtistViewHolder artistViewHolder = (ArtistViewHolder) holder;
            GlideApp.with(holder.itemView).clear(artistViewHolder.getArtistImage());
        }
    });
    FixedPreloadSizeProvider<MediaCover> coverSizeProvider = new FixedPreloadSizeProvider<>(64, 64);
    RecyclerViewPreloader<MediaCover> preloader = new RecyclerViewPreloader<MediaCover>(GlideApp.with(this), mArtistsAdapter, coverSizeProvider, 2);

    return rootView;
}

作为奖励,我为我的MediaCover添加了抓取器:

/**
 * Created by vinz243 on 13/12/2017.
 */

public class MediaCoverFetcher implements DataFetcher<InputStream> {
    private final MediaCover mediaCover;
    private boolean cancelled = false;
    private InputStream stream;

    public MediaCoverFetcher(MediaCover mediaCover) {
        this.mediaCover = mediaCover;
    }

    @Override
    public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
        CompactdArtwork artwork = mediaCover.getArtwork();
        try {
            stream = artwork.getImage(ArtworkSize.LARGE);
            if (cancelled) {
                callback.onLoadFailed(new RuntimeException("Cancelled"));
            } else {
                callback.onDataReady(stream);
            }
        } catch (NullPointerException e) {
            callback.onLoadFailed(e);
        }
    }

    @Override
    public void cleanup() {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void cancel() {
        cancelled = true;
    }

    @NonNull
    @Override
    public Class<InputStream> getDataClass() {
        return InputStream.class;
    }

    @NonNull
    @Override
    public DataSource getDataSource() {
        return DataSource.LOCAL;
    }

}

我猜它来自回收部分,但我不知道如何解决这个问题。正如您在代码中看到的那样,我尝试了setIsRecyclable,但我会收到错误,说出无法匹配的调用,而且无法修复它

1 个答案:

答案 0 :(得分:1)

你应该尝试改变一些事情:

  • 在setResource函数
  • 中移动ArtistViewHolder对象上的setter
  • 将您的CompactdArtist变量定义为最终

    @Override
        public void onBindViewHolder(final ArtistViewHolder holder, int position)       {
        Log.d(TAG, "onBindViewHolder: " + position);
        final CompactdArtist current = artists.get(position);
    
        // holder.setIsRecyclable(false);
        holder.bindArtist(artists.get(position));
    
        holder.artistImageView.setImageDrawable(null);
        try {
         current.fetch();
         fullRequest.load(new MediaCover(current))
                .fallback(ImageUtils.getFallback(mContext))
                .into(new ImageViewTarget<Bitmap>(holder.artistImageView) {
                    @Override
                    protected void setResource(@Nullable Bitmap resource) {
                        if (resource == null) {
                            // holder.setIsRecyclable(true);
                            return;
                        }
    
                        holder.artistImageView.setImageBitmap(resource);
                        int color = Palette.from(resource).generate().getMutedColor(0xFFFFFF);
                        holder.artistBackground.setBackgroundColor(color);
    
                        // holder.setIsRecyclable(true);
    
                         holder.artistNameText.setText(current.getName());
                         holder.artistSub.setText(current.getAlbumCount() + " albums");
                    }
    
                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        super.onLoadCleared(placeholder);
                        // holder.setIsRecyclable(true);
                    }
                });
       } catch (CouchbaseLiteException e) {
          e.printStackTrace();
          holder.artistNameText.setText(R.string.unknown_artist_name);
       } catch (IOException e) {
         e.printStackTrace();
     }
    }
    

我认为你加载图像的方式是异步的,所以在这种情况下你的问题在于你将变量设置为同步过程,这就是为什么你把信息显示在错误的块上