简体   繁体   English

带滑行的回收器视图

[英]Recycler view with glide

I am facing following problems with given code:我面临给定代码的以下问题:

  1. Images reload when I scroll back the recyclerview当我回滚 recyclerview 时重新加载图像
  2. Wrong thumbnails are loaded(they keep changing when I scroll back and forth)加载了错误的缩略图(当我来回滚动时它们会不断变化)
  3. UI hangs(I tried threading but problem is still there) UI挂起(我尝试过线程,但问题仍然存在)

These problems are not present for video files(else part of outer if)视频文件不存在这些问题(外部 if 的其他部分)

Inside videoadapter:内部视频适配器:

if(videoFiles.get(position).getType().equals(MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO+"")){
            if(coverpicture(videoFiles.get(position).getPath())!=null) {
                        Glide.with(mContext)
                                .load(coverpicture(videoFiles.get(position).getPath()))
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .placeholder(circularProgressDrawable)
                                .into(holder.thumbnail);
            }
            else {
                        Glide.with(mContext)
                                        .load(new File(videoFiles.get(position).getPath()))
                                        .placeholder(R.drawable.ic_baseline_music_note)
                                        .into(holder.thumbnail);
            }
} else {
            Glide.with(mContext)
                    .load(new File(videoFiles.get(position).getPath()))
                    .placeholder(circularProgressDrawable)
                    .into(holder.thumbnail);
}

This is how I call videoadapter:这就是我调用视频适配器的方式:


videoAdapter = new VideoAdapter(getActivity(),videoFiles);

The function coverpicture:功能封面图:

private Bitmap coverpicture(String path) {
        final MediaMetadataRetriever[] mr = new MediaMetadataRetriever[1];
        final byte[][] byte1 = new byte[1][1];
        Thread ttt = new Thread(){
            @Override
            public void run() {
                super.run();
                mr[0] = new MediaMetadataRetriever();
                mr[0].setDataSource(path);
                byte1[0] = mr[0].getEmbeddedPicture();
                mr[0].release();

            }
        };
        ttt.start();
        while(true){
            if(!ttt.isAlive()){
                if(byte1[0] != null) {
                    return BitmapFactory.decodeByteArray(byte1[0], 0, byte1[0].length);
                }
                else {
                    return null;
                }
            }
        }
    }

EDit: wrong thumbnail problem is solved when I replace first glide statement with编辑:当我用第一个 glide 语句替换时,错误的缩略图问题解决了

(it would be useful if someone explained why) (如果有人解释原因会很有用)

Glide.with(mContext)
          .load(coverpicture(videoFiles.get(position).getPath()))
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .placeholder(circularProgressDrawable)
                                .into(new DrawableImageViewTarget(holder.thumbnail));
  1. In recyclerview adapter's onBindViewHolder , in a background thread, load the glide images first.Then show it using view.post在 recyclerview 适配器的onBindViewHolder 中,在后台线程中,首先加载滑动图像。然后使用view.post显示它
    new Thread(()->{
        Bitmap b =coverpicture(videoFiles.get(position).getPath());
        Glide.with(mContext).downloadOnly().load(b);
          holder.thumbnail.post(() -> {
           Glide.with(mContext).clear(holder.thumbnail);
           Glide.with(holder.thumbnail.getContext())
                          .load(new File(videoFiles.get(position).getPath()))
                          .placeholder(circularProgressDrawable)
                          .format(DecodeFormat.PREFER_RGB_565)
                          .into(new DrawableImageViewTarget(holder.thumbnail));

         }
   }).start();          

2 . 2 . Already mentioned in the post帖子里已经提到了

3 . 3 . Changed coverpicture() to封面图片()更改为

   static Bitmap coverpicture(String path) {
        MediaMetadataRetriever mr;
        byte[] byte1 = new byte[1];
        mr = new MediaMetadataRetriever();
        mr.setDataSource(path);
        try {
            byte1 = mr.getEmbeddedPicture();
            mr.release();
        }catch (Exception e){
            e.printStackTrace();
        }
        if(byte1 != null) {
            return BitmapFactory.decodeByteArray(byte1, 0, byte1.length);
        }
        else {
            return null;
        }
    }

No need of threading inside coverpicture() as it is called from a background thread不需要在coverpicture()内部穿线,因为它是从后台线程调用的

ALSO, the following optimistaions in recyclerview will help此外,recyclerview 中的以下优化将有所帮助

recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM