android ListView只加載當(dāng)前屏幕顯示的圖片(解決List滑動時加載卡頓)
之前寫過一個關(guān)于ListView異步加載網(wǎng)絡(luò)圖片的文章:http://blog.sina.com.cn/s/blog_7033e38201010req.html,但是使用時發(fā)現(xiàn)如果快速滑動List,因?yàn)檎诩虞d圖片,會有點(diǎn)卡。這時我們就希望能夠只加載當(dāng)前屏幕顯示的Item的圖片。
本文引用地址:http://www.ex-cimer.com/article/201809/388380.htm廢話不多少,直接粘貼代碼:
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
// 異步加載圖片
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {//list停止?jié)L動時加載圖片
pageImgLoad(_start_index, _end_index);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
//設(shè)置當(dāng)前屏幕顯示的起始index和結(jié)束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);
}
}
}
評論