Bitmap bitmap=cache.get(
""
+position);
//取出缓存中的bitmap
final
ViewHolder holder;
if
(convertView==
null
){
holder=
new
ViewHolder();
convertView=getLayoutInflater().inflate(R.layout.item,
null
);
holder.txt=(TextView) convertView.findViewById(R.id.txt);
holder.img=(ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder) convertView.getTag();
}
if
(bitmap==
null
){
//如果没有缓存
if
(!isBusy){
//判断当前listview不在滑动状态
Log.d(
"=========="
,
"正常加载当前位置图片:"
+position);
executor.execute(
new
Runnable() {
//异步加载图片
@Override
public
void
run() {
Bitmap bitmap=DownLoadImage();
//模拟下载图片
if
(bitmap!=
null
){
cache.put(
""
+position, bitmap);
Message message=handler.obtainMessage();
message.obj=holder.img;
message.arg1=position;
message.what=LOADIMAGE;
handler.sendMessage(message);
//图片下载完成后通知更新
}
}
});
}
else
{
//如果当前listview在快速滑动状态
Message message=handler.obtainMessage();
message.obj=holder.img;
message.arg1=position;
message.what=LOADIMAGE;
messages.add(message);
///记录当前getview的图片资源信息,保存在message中,放入到集合等待加载
}
holder.img.setImageBitmap(defaultBitmap);
//设置默认图片,等图片资源下载完毕后在更新
}
else
{
holder.img.setImageBitmap(bitmap);
}
holder.txt.setText(position+
""
);
return
convertView;