我在我的项目中使用适用于 Android 的 Picasso 图像加载库。当我看到它将错误的图像加载到利用 ViewHolder 模式的 ArrayAdapter 中的 ImageView 时,我注意到了这个错误。
这是错误的症结所在,我现在可以在我创建的测试活动中始终如一地重现该错误。我在 onCreate 中调用这个方法:
private void refreshImageView() {
ImageView imageView = (ImageView)this.findViewById(R.id.image);
for (int i = 0; i < 10; i++) {
new Picasso.Builder(this).build()
.load(Uri.parse("http://192.168.1.1:4568/images/red.png"))
.placeholder(R.drawable.silhouette)
.noFade()
.into(imageView);
}
new Picasso.Builder(this).build()
.load(Uri.parse("http://192.168.1.1:4568/images/blue.png"))
.placeholder(R.drawable.silhouette)
.noFade()
.into(imageView);
}
我还向 ImageViewAction.java 添加了一些调试输出。这是我添加的内容:
String str = bitmapToString(result);
Log.i("PicassoDebug", String.format("Complete and loading %s into %s", str.substring(str.length() - 20, str.length() - 1), target));
这是输出:
12-10 17:08:37.061: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.061: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ........ 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.121: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.121: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.171: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.171: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.221: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.221: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.281: D/dalvikvm(16411): GC_FOR_ALLOC freed 1766K, 22% free 16413K/20988K, paused 32ms, total 33ms
12-10 17:08:37.281: I/PicassoDebug(16411): Complete and loading ewAAAAASUVORK5CYII= into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.341: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.341: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ........ 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.391: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.391: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.441: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.441: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.501: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.501: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.551: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.551: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ........ 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.601: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.601: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
以“nbAA”开头的行是red.png,以“ewAA”开头的行是blue.png。如您所见,红色的一些加载请求在蓝色之后完成,因此显示红色而不是蓝色。
毕加索有错误吗?还是这是“设计使然”?如果是设计使然,那么在 ArrayAdapters 中使用 Picasso 的建议设计模式是什么,在更新之前经常会在同一个项目上重复调用 getView()?