参考 http://www.3lian.com/edu/2013/08-03/86585.html
说明了复用convertview的后果,并给出了解决办法。通过给imageview设置tag,在线程更新时判断tag来觉得是否显示图片。
但这样并不能解决问题,因为快速滑动后convertview 会把之前position的那个完全复制过来,所以要想显示图片不错位,只要先隐藏imageview就行了。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflate.inflate(R.layout.note_list_item, parent,
false);
holder = new ViewHolder();
holder.img = (ImageView) convertView
.findViewById(R.id.note_listitem_img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.img.setVisibility(View.INVISIBLE);
holder.position = position;
...
holder.img.setTag(Integer.valueOf(position));
String path = nr.imgpath;
if (path != null && (!path.equals("null"))) {
LayoutParams param = holder.titleAndContent.getLayoutParams();
// Log.d("param", "param.width = " + param.width);
param.width = 500;
// img.setVisibility(View.INVISIBLE);
if (listitem_imgs.get(position) == null) {
// Log.d("test", "" + position + " excute task");
new LoadSingleImageTask(position, path, holder.img)
.execute();
} else {
// Log.d("test", "position "+position+" not async show");
ListItemImg item = listitem_imgs.get(position);
if (item.bm != null) {
holder.img.setImageBitmap(item.bm);
holder.img.setVisibility(View.VISIBLE);
}
}
} else {
LayoutParams param0 = holder.img.getLayoutParams();
holder.img.setVisibility(View.GONE);
LayoutParams param1 = holder.titleAndContent.getLayoutParams();
// Log.d("param", "param.width = " + param.width);
param1.width = 500 + param0.width;
// img.setLayoutParams(null);
}
亲测这样就不会显示之前position的图片了,但是用asynctask做异步效果不是很好,以后可以用线程池试下后再得出结论。
至于不复用convertview 会出现bug ,图片可能会显示不全。