Recyclerview Images Misplacing and disappear while scrolling up/down

本小妞迷上赌 提交于 2019-12-24 05:48:21

问题


I'm retrieving Video thumbnails and displaying it in recycledview's imageview, problem that I'm facing with was Image's getting misplaced and disappeared while Scrolling Up/Down. When I'm using runnable class Recyclarview gets freezes.

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");
  }

 }

回答1:


Use AsynTask inside the Viewholder

 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());



回答2:


Try using picasso for loading images inside RecyclerView. And the issue you are facing is because recyclerview recycles the view inside onBindView method. Thus you need to keep a week reference to those objects. Which picasso does perfectly. (it also recycles your bitmaps :) )




回答3:


In your bindData method, you need to add this line

photoHolder.setIsRecyclable(false);



回答4:


you can use picasso to download image fromurl and set it in image under recyclerview item.

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

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



来源:https://stackoverflow.com/questions/42943593/recyclerview-images-misplacing-and-disappear-while-scrolling-up-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!