我尝试在新线程中移动我的加载逻辑,而RecyclerImageViewHolder中的ImageView变化非常快。我尝试中断线程,我在设置图像之前尝试检查,但它不起作用...我不明白为什么它不起作用
//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);
}
}
});
}
答案 0 :(得分:2)
错误与RecyclerView
重用其ViewHolder
的方式有关..
基本上,这里是您视图中任意ViewHolder
发生的事情的简要说明。
这就是滚动后加载多个图像的原因。
对此没有简单的解决方法。如果ViewHolder
的基础内容发生更改,则需要告知您的图像下载线程停止。