Android在ListView显示图片(重复错乱闪烁问题)
1、原因分析
n为一屏最多显示ListView 行item的个数。
@Override
public
View getView (
int
position , View convertView , ViewGroup parent ) {
ViewHolder holder ;
if
( convertView ==
null
) {
convertView = inflater . inflate ( R . layout . list_item ,
null
) ;
holder =
new
ViewHolder ( ) ;
……
convertView . setTag ( holder ) ;
}
else
{
holder = ( ViewHolder ) convertView . getTag ( ) ;
}
}
/**
* 显示这个图片,解决了列表问题.
* 列表问题:滑动过程中,getView的imageView会重复利用,导致图片会串位
* @param imageView 显得的View
* @param url the url
* @return
*/
public
void
display(
final
ImageView imageView,String url) {
if
(AbStrUtil.isEmpty(url)){
if
(noImage !=
null
){
if
(loadingView !=
null
){
loadingView.setVisibility(View.INVISIBLE);
imageView.setVisibility(View.VISIBLE);
}
imageView.setImageDrawable(noImage);
}
return
;
}
//设置下载项
final
AbImageDownloadItem item =
new
AbImageDownloadItem();
//设置显示的大小
item.width = width;
item.height = height;
//设置为缩放
item.type = type;
item.imageUrl = url;
final
String cacheKey = AbImageCache
.getCacheKey(item.imageUrl, item.width, item.height, item.type);
item.bitmap = AbImageCache.getBitmapFromCache(cacheKey);
//if(D) Log.d(TAG, "缓存中获取的"+cacheKey+":"+item.bitmap);
//设置标记
imageView.setTag(url);
if
(item.bitmap ==
null
){
//先显示加载中
if
(loadingView!=
null
){
loadingView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.INVISIBLE);
}
else
if
(loadingImage !=
null
){
imageView.setImageDrawable(loadingImage);
}
//下载完成后更新界面
item.setListener(
new
AbImageDownloadListener() {
@Override
public
void
update(Bitmap bitmap, String imageUrl) {
//未设置加载中的图片,并且设置了隐藏的View
if
(loadingView !=
null
&& imageUrl.equals(imageView.getTag())){
loadingView.setVisibility(View.INVISIBLE);
imageView.setVisibility(View.VISIBLE);
}
//要判断这个imageView的url有变化,如果没有变化才set,
//有变化就取消,解决列表的重复利用View的问题
if
(bitmap!=
null
&& imageUrl.equals(imageView.getTag())){
if
(D) Log.d(TAG,
"图片下载,设置:"
+imageUrl);
imageView.setImageBitmap(bitmap);
}
else
{
if
(errorImage !=
null
&& imageUrl.equals(imageView.getTag())){
imageView.setImageDrawable(errorImage);
}
}
}
});
if
(D) Log.d(TAG,
"图片下载,执行:"
+url);
mAbImageDownloadPool.execute(item);
}
else
{
if
(loadingView !=
null
){
loadingView.setVisibility(View.INVISIBLE);
imageView.setVisibility(View.VISIBLE);
}
imageView.setImageBitmap(item.bitmap);
}
}
andbase 介绍:http://blog.csdn.net/menglele1314/article/details/46422409
下载:http://download.csdn.net/detail/menglele1314/8786989