2017-04-15 116 views
0

我正在使用RealmRecyclerViewAdapter來保存Series對象的集合,這些對象具有與它們關聯的圖像。當我刷新Series數據(通過API)時,適配器自動更新,如我所料,但顯示的圖像更改並且不正確。通過我的測試,我發現通過RealmRecyclerViewAdapter構造函數將自動更新設置爲false可以停止這種行爲,因此刷新列表項會導致這種情況發生。另外如果我離開auto-updatetrue,並且我在onBindViewHolder的調用中設置了一個斷點,圖像仍然是正確的。無斷點運行代碼(全速)將導致顯示不正確的圖像。奇怪的是,只有在更新後圖像不正確顯示。任何文字仍然準確。自動更新RealmRecyclerViewAdapter在更新上顯示不正確的數據

這是顯示此行爲的GIF。正如你所看到的,當我刷新數據時,行被更新並顯示不正確的圖像。

https://gyazo.com/88d5735796770b1573bdd518ce53ed44

這裏是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動漫。如果沒有,它將檢查緩存版本是否可用。

回答

2
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'