如果图像尚不存在,我会尝试下载它们;如果图像已经下载,我会保留并重新使用它们。
下载和展示作品时,重新使用它们却不会。看来不同调用中的 mValues
不是相同的 mValues
。
这是我的代码
public void onBindViewHolder(final TabFragment1.SimpleItemRecyclerViewAdapter.ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).title);
holder.mContentView.setText(mValues.get(position).description);
if (holder.mThumbnail == null) {
System.out.println("thumbnail null"); //NEVER gets called
} else {
if(mValues.get(position).thumbnailData!=null){
System.out.println("thumbnailData contained"); //NEVER GETS CALLED, IT ALWAYS SEEMS TO BE NUL
holder.mThumbnail.setImageDrawable(mValues.get(position).thumbnailData);
} else {
System.out.println("thumbnailData downloading"); //always happening
try {
AsyncTask<String, Void, Bitmap> execute = new DownloadImageTask((ImageView) holder.mThumbnail)
.execute(holder.mItem.thumbnailUrl);
mValues.get(position).thumbnailData = holder.mThumbnail.getDrawable();
if(mValues.get(position).thumbnailData!=null) {
System.out.println("is null"); //hardly ever gets printed
} else {
System.out.println("is not null"); //always gets printed
}
} catch (Exception ex) {
}
}
}
}
已解决:我的代码错误。缩略图更新没有等待 AsyncTask 完成,我搞乱了 if 语句,所以它欺骗了我,让我认为"new"缩略图有效。我将缩略图更新放入 AsyncTask 中,现在它正在工作:)
最佳答案
你的 if else 语句,在 try catch 中已经交换了日志语句...这里你的 if 语句检查 (thumbnail !=null) 但如果找到 true 则将其打印为 null...所以我的观点是,你的缩略图object 始终为 null,因为它的值取决于您在其上方启动的 Asynctask 的完成情况,因此您无法像这样访问它,因为此下载过程将在单独的线程上运行。
关于Android:onBindViewHolder无法缓存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268600/