Recyclerview 图片在滚动时错位并消失 up/down

Recyclerview Images Misplacing and disappear while scrolling up/down

我正在检索视频缩略图并将其显示在 recycledview 的图像视图中,我面临的问题是图像在滚动时错位并消失 Up/Down。当我使用 runnable class 时,Recyclarview 会冻结。

private static Bitmap retrieveVideoFrameFromVideo(String videoPath) {
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
  mediaMetadataRetriever = new MediaMetadataRetriever();
  if (Build.VERSION.SDK_INT >= 14) {
    mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
  } else {
    mediaMetadataRetriever.setDataSource(videoPath);
  }
  //   mediaMetadataRetriever.setDataSource(videoPath);
  bitmap = mediaMetadataRetriever.getFrameAtTime();
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (mediaMetadataRetriever != null) {
    mediaMetadataRetriever.release();
  }
}
return bitmap;
 }

 @Override void bindData(RecyclerView.ViewHolder holder, final MediaResponse  data) {
final PhotoHolder photoHolder = (PhotoHolder) holder;
photoHolder.mTitle.setText(BasicUtils.removeDoubleQuotes(data.getTitle()));
photoHolder.mPostedBy.setText(data.getPostedby());
photoHolder.upvote.setText(String.valueOf(data.getLikes()));
photoHolder.date.setText(
        BasicUtils.parseDate(data.getDatetime(),  AppConstantsUtils.FROM_DATE_FORMAT,
                AppConstantsUtils.TO_DATE_FORMAT));


new BackTask(photoHolder.mMediaPic).execute(
       AppConstantsUtils.BASE_URL + data.getMedia());




 }

 private class BackTask extends AsyncTask<String, Void, Bitmap> {
  private ImageView imageView;
  BackTask(ImageView imageView) {
  this.imageView = imageView;
}

   @Override protected Bitmap doInBackground(String... params) {
  // return retrieveVideoFrameFromVideo(params[0]);
  Log.d("URLHARRY",params[0]);
  // return ThumbnailUtils.createVideoThumbnail(params[0],            MediaStore.Video.Thumbnails.MINI_KIND );
  return retrieveVideoFrameFromVideo(params[0]);
}

  @Override protected void onPostExecute(Bitmap bitmap) {
  super.onPostExecute(bitmap);

  if(bitmap!=null) {
    imageView.setImageBitmap(bitmap);
  }
  else{
    imageView.setImageBitmap(null);
    ToastUtils.showToast(context,"Hai");
  }

 }

在你的bindData方法中,你需要添加这一行

photoHolder.setIsRecyclable(false);

尝试使用 picassoRecyclerView 中加载图像。你面临的问题是因为 recyclerview 在 onBindView 方法中回收视图。因此,您需要保留对这些对象的一周引用。毕加索做得很完美。 (它还会回收您的位图 :))

你可以使用picasso从url下载图片,并在recyclerview项下的图片中设置。

Picasso.with(上下文) .load(url) .placeholder(R.drawable.user_placeholder) .error(R.drawable.user_placeholder_error) .into(imageView);

http://square.github.io/picasso/

在 Viewholder 中使用 AsyncTask

 new AsyncTask<String, String, String>() {
    Bitmap bitmapVideo;
    String location;


    @Override
    protected String doInBackground(String... strings) {
      try {
        //Your method call here
        bitmapVideo = retrieveVideoFrameFromVideo(strings[0]);
        location=strings[0];
        //  addBitmapToMemoryCache(location, bitmapVideo);

      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
      return null;
    }

    @Override
    protected void onPostExecute(String id) {
      super.onPostExecute(id);

      if (bitmapVideo != null) {
        //Load your bitmap here
        photoHolder.mMediaPic.setImageBitmap(bitmapVideo);
        addBitmapToMemoryCache(location, bitmapVideo);


      } else {
        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ananda);
        photoHolder.mMediaPic.setImageBitmap(largeIcon);
        addBitmapToMemoryCache(location, largeIcon);

      }
    }
  }.execute(AppConstantsUtils.BASE_URL + data.getMedia());