【问题标题】:Auto-updating RealmRecyclerViewAdapter displays incorrect data on update自动更新 RealmRecyclerViewAdapter 在更新时显示不正确的数据
【发布时间】:2017-04-15 11:19:56
【问题描述】:

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

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

这是onBindViewHolder 的 sn-p,其中设置了行的图像:

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 的动画。如果没有,它将检查缓存版本是否可用。

【问题讨论】:

    标签: android android-recyclerview realm realm-mobile-platform


    【解决方案1】:
    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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-04
      • 2018-10-08
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多