之前写过一个关于ListView异步加载网络图片的文章:http://blog.sina.com.cn/s/blog_7033e38201010req.html,可是使用时发现假如快速滑动List,由于正在加载图片,会有点卡。这时咱们就期望可以只加载当时屏幕显现的Item的图片。
废话不多少,直接张贴代码:
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
// 异步加载图片
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {//list中止翻滚时加载图片
pageImgLoad(_start_index, _end_index);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
//设置当时屏幕显现的开始index和完毕index
_start_index = firstVisibleItem;
_end_index = firstVisibleItem + visibleItemCount;
if (_end_index >= totalItemCount) {
_end_index = totalItemCount – 1;
}
}
});
private void pageImgLoad(int start_index, int end_index) {
for (; start_index end_index; start_index++) {
HashMap
if (curr_item.get(Constant.NEWS_ICON_URL) != null
curr_item.get(Constant.NEWS_ICON) == null) {
loadImage(curr_item);
}
}
}