您的位置:首页 > 移动开发 > Android开发

Android ListView及异步加载图片问题汇总

2014-10-05 15:30 204 查看
Android Listview是我在开发中用的比较多的复杂控件, 从最初不懂重用convertview到现在解决异步加载图片位置错乱等问题已经有三个年头了。现在总算对基本的一些用法给常见的难解问题有了一定的了解,下面罗列一些常见问题及解决方法。

1. Listview重用convertview

这个是最开始使用Listview需要解决的问题,如果convertview不重用,如果数据过多会带来很大的性能问题。所以用自定义Listview必须重用convertview。

参考代码在问题2中。

2.异步加载图片错位的问题:

比较巧妙的方法是使用setTag()方法。

[java] view
plaincopy

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {

convertView = inflator.inflate(R.layout.article_row, null);

TextView nameTextView = (TextView) convertView

.findViewById(R.id.title);

TextView bottomText = (TextView) convertView

.findViewById(R.id.bottomtext);

ImageView iconView = (ImageView) convertView

.findViewById(R.id.article_icon);

holder = new ViewHolder();

holder.nameTextView = nameTextView;

holder.bottomText = bottomText;

holder.iconView = iconView;

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

Article article = articles.get(position);

holder.nameTextView.setText(article.getTitle());

holder.bottomText.setText(article.getAuthor() + " | "

+ article.getPubDate());

<strong> holder.iconView.setTag(URLS[position]);</strong> // 在BitmapManager加载图片完毕以后通过url找这个对象, 然后加载图片.

BitmapManager.INSTANCE.loadBitmap(URLS[position], holder.iconView, 32,

32);

return convertView;

}

}

参考连接:http://www.cnblogs.com/liongname/articles/2345087.html
http://negativeprobability.blogspot.com/2011/08/lazy-loading-of-images-in-listview.html
3.加载图片的时候如果滑动就会出现卡的现象,这种时候需要判断是否在滑动,如果滑动就暂停加载图片。用thread的lock机制来解决。

连接: http://www.iteye.com/topic/1118828
4.异步加载网络图片,最好做二级缓存,在本地保存一分备份儿,如果在本地查找不到以后再去网上下载。

参考连接:http://blog.csdn.net/zircon_1973/article/details/7693839

5.当listview有Header或者Footer的时候,position有问题。

http://blog.chengbo.net/2012/03/09/onitemclick-return-wrong-position-when-listview-has-headerview.html

http://blog.iamzsx.me/show.html?id=147001

6.由getItemViewType返回对应项的自定义视图类型,getViewTypeCount返回视图类型总数。

注意:getViewTypeCount返回的值必须比视图类型常量值大,以数组来比喻的话,getViewTypeCount返回的是数组的长度,getItemViewType返回的(即3.1.1中定义的常量)就是数组的下标。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: