Recyclerview图像在上/下滚动时错位并消失 [英] Recyclerview Images Misplacing and disappear while scrolling up/down
本文介绍了Recyclerview图像在上/下滚动时错位并消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在检索视频缩略图并将其显示在回收视图的imageview中,我面临的问题是向上/向下滚动时图像放错了位置并消失了.当我使用可运行类时,Recyclarview会冻结.
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");
}
}
推荐答案
在Viewholder中使用AsynTask
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());
这篇关于Recyclerview图像在上/下滚动时错位并消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文