其他分享
首页 > 其他分享> > android-从回收站中的Firebase存储中加载图像会显示图像更改

android-从回收站中的Firebase存储中加载图像会显示图像更改

作者:互联网

我正在使用recyclerview从火力基地存储中加载图像,但是当我加载说图像A滚动到该图像视图被回收的位置并将用于显示图像B时,在更改为图像之前,我仍然可以短暂地看到图像A B,

有办法避免这种情况吗?

Here is the code for my adapter

public class ComicRecyclerAdapter extends RecyclerView.Adapter<ComicRecyclerAdapter.ComicHolder> {


public ComicRecyclerAdapter(ArrayList<Comic> comicsInSection) {
    this.mComicsInSection = comicsInSection;
}

@Override
public ComicHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //Bind the Comic object to the ComicHolder
    View comicView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.layout_grid_comic, parent, false);

    return new ComicRecyclerAdapter.ComicHolder(comicView);
}

@Override
public void onBindViewHolder(final ComicHolder holder, int position) {
    Comic currentComic = mComicsInSection.get(position);

    storageRef.child(currentComic.getTitle() + "/"
            + currentComic.getCoverImage()).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            //Load image using Picasso
            Picasso.get()
                    .load(uri)
                    .fit()
                    .into(holder.coverImage, new Callback() {
                        @Override
                        public void onSuccess() {
                            //image loaded stop loading icon
                            holder.loadingComic.setVisibility(View.GONE);
                        }

                        @Override
                        public void one rror(Exception e) {

                        }
                    });
        }
    });
}

@Override
public int getItemCount() {
    return (null != mComicsInSection ? mComicsInSection.size() : 0);
}

public void addAll(ArrayList<Comic> comicsToAdd) {
    mComicsInSection.addAll(comicsToAdd);
    notifyDataSetChanged();
}

public void clear() {
    mComicsInSection.clear();
    notifyDataSetChanged();
}

static class ComicHolder extends RecyclerView.ViewHolder {
    //Find all views needed
    @BindView(R.id.comic_cover_image)
    ImageView coverImage;
    @BindView(R.id.loading_comic)
    ProgressBar loadingComic;

    public ComicHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

}

解决方法:

我建议您先获取图像URL列表,然后转到适配器并仅使用Picasso加载它.现在发生的情况是,当滚动回收器视图时,您可能会首先获得第3个位置的URL,而不是第1个位置的URL,这可能会导致您产生意外的结果.

标签:firebase,android-recyclerview,firebase-storage,android
来源: https://codeday.me/bug/20191109/2011335.html