自动更新 RealmRecyclerViewAdapter 在更新时显示不正确的数据

Auto-updating RealmRecyclerViewAdapter displays incorrect data on update

我正在使用 RealmRecyclerViewAdapter 来保存 Series 对象的集合,这些对象具有与之关联的图像。当我刷新 Series 数据(通过 API)时,适配器会按我的预期自动更新,但显示的图像已更改且不正确。通过我的测试,我注意到通过 RealmRecyclerViewAdapter 构造函数将自动更新设置为 false 会停止这种行为,因此列表项的刷新会导致这种情况发生。此外,如果我将 auto-update 保留为 true,并且在调用 onBindViewHolder 期间设置断点,图像仍然正确。 运行 没有断点的代码(全速)会导致显示不正确的图像。奇怪的是,更新后只有图像显示不正确。任何文本仍然准确。

这是显示此行为的 GIF。如您所见,当我刷新数据时,行会更新并显示不正确的图像。

这是 onBindViewHolder 的片段,其中设置了行的图像:

public void onBindViewHolder(ViewHolder holder, int position) {
    final int adapterPosition = holder.getAdapterPosition();
    holder.series = getItem(adapterPosition);

    final String MALID = holder.series.getMALID();

    ...

    int imageId = App.getInstance().getResources().getIdentifier("malid_" + holder.series.getMALID(), "drawable", "<app-package>");
    if (imageId != 0) {
        Picasso.with(App.getInstance()).load(imageId).fit().centerCrop().into(holder.mPoster);
    } else {
        File cacheDirectory = App.getInstance().getCacheDir();
        File bitmapFile = new File(cacheDirectory, holder.series.getMALID() + ".jpg");

        Picasso.with(App.getInstance()).load(bitmapFile).placeholder(R.drawable.placeholder).fit().centerCrop().into(holder.mPoster);
    }

    holder.mAddButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addSeriesHelper(MALID);
        }
    });
    holder.mMinusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RemoveSeriesDialogFragment dialogFragment = RemoveSeriesDialogFragment.newInstance(self, MALID, adapterPosition);
            dialogFragment.show(seriesFragment.getMainActivity().getFragmentManager(), TAG);
        }
    });
}

我首先检查应用程序资源是否包含 Series 的动漫。如果没有,它将检查缓存版本是否可用。

if (!(photoPath.isEmpty())) {

            int defaultImagePath = R.drawable.default_thumb;
            int errorImagePath = R.drawable.damaged_image;
            holder.mImageView.setImageResource(defaultImagePath);

            String uri = photoPath;
            loadImageWithGlide(mContext, holder.mImageView, uri, defaultImagePath,
                    errorImagePath);
        }


        public static void loadImageWithGlide(final Context context, ImageView theImageViewToLoadImage,
                                          String theLoadImagePath, int theDefaultImagePath, int tehErrorImagePath) {
        if (context == null) return;

        //placeHolderUrl=R.drawable.ic_user;
        //errorImageUrl=R.drawable.ic_error;
        Glide.with(context) //passing context
                .load(theLoadImagePath) //passing your url to load image.
                .placeholder(theDefaultImagePath) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                .error(tehErrorImagePath)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                //.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                .fitCenter()//this method help to fit image into center of your ImageView
                .into(theImageViewToLoadImage); //pass imageView reference to appear the image.

    }

    add dependencies inside app build.gralde.
     // glide
    compile 'com.github.bumptech.glide:glide:3.7.0'