RecyclerImageViewHolder在新线程中加载项目

时间:2016-04-18 13:08:09

标签: android multithreading android-recyclerview

我尝试在新线程中移动我的加载逻辑,而RecyclerImageViewHolder中的ImageView变化非常快。我尝试中断线程,我在设置图像之前尝试检查,但它不起作用...我不明白为什么它不起作用

demonstration

 //One view - one thread
    private Thread imageLoadThread = null;

    public AuthorRecyclerViewHolder(LinearLayout itemView, Activity activity) {
        super(itemView);
        this.view = itemView;
        itemView.findViewById(R.id.card_view).setOnClickListener(this);
        this.activity = activity;
    }

    public AuthorRecyclerViewHolder setItem(AuthorObject ao) {
        this.ao = ao;

        //Find image
        iv = (ImageView) view.findViewById(R.id.imageView);

        //Clear
        if (imageLoadThread != null)
            imageLoadThread.interrupt();
        iv.clearAnimation();
        iv.setImageResource(R.drawable.notfoundmusic);

        //Set content
        setImageOnItemView(activity, iv, false);
        ((TextView) view.findViewById(R.id.description)).setText(ao.description);
        ((TextView) view.findViewById(R.id.head_author)).setText(ao.name);
        return this;
    }


    public void setImageOnItemView(final Activity activity, final ImageView iv, boolean isBigPicture) {
        final Animation vis = AnimationUtils.loadAnimation(activity, R.anim.alphavisible);
        final ImageResource ir = isBigPicture ? ao.bigImage == null ? ao.smallImage : ao.bigImage : ao.smallImage == null ? ao.bigImage : ao.smallImage;
        if (ir != null)
            this.imageLoadThread = ir.getImage(this);
        else {
            iv.setImageResource(R.drawable.notfoundmusic);
            iv.startAnimation(vis);
            iv.setAlpha(1.0F);
        }
    }


    @Override
    public void recieveResource(final @Nullable Bitmap bitmap, final String id) {
        final Thread thisThread = Thread.currentThread();

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                final Animation vis = AnimationUtils.loadAnimation(activity, R.anim.alphavisible);
                final Animation unvis = AnimationUtils.loadAnimation(activity, R.anim.alphaunvisible);
                iv.clearAnimation();

                if (thisThread.isInterrupted())
                    return;
                if (id.substring(id.lastIndexOf("_") + 1).equals(String.valueOf(ao.authorId)))
                    if (bitmap != null) {
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                iv.setImageBitmap(bitmap);
                                iv.setAnimation(vis);
                            }
                        }, unvis.getDuration());
                        iv.setAlpha(1.0F);
                        iv.startAnimation(unvis);
                    } else {
                        iv.startAnimation(vis);
                        iv.setAlpha(1.0F);
                    }
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

错误与RecyclerView重用其ViewHolder的方式有关..

基本上,这里是您视图中任意ViewHolder发生的事情的简要说明。

  1. 创建
  2. 填充数据(图像线程开始下载图像#1)
  3. 向下滚动
  4. 从数据集填充的新数据(图像线程开始下载图像#2)
  5. 向下滚动
  6. 从数据集填充的新数据(图像线程开始下载图像#3)
  7. 您停止滚动
  8. 图片#1已下载并显示
  9. 图片#2已下载并显示
  10. 图片#3已下载并显示
  11. 这就是滚动后加载多个图像的原因。

    对此没有简单的解决方法。如果ViewHolder的基础内容发生更改,则需要告知您的图像下载线程停止。