最近在做ListView分頁顯示,其中包括圖片 和文字(先下載解析文字內容,再非同步載入圖片)發現每次點擊下一頁後,文字內容載入完畢,馬上向下滑動,由於這時後台在用線程池非同步下載圖片,我每頁有20條,也就是20張圖片,會導致listview滑動卡頓!
這是使用者不想看到的,我參考了網易新聞和電子市場等應用,發現它們都是只載入螢幕內的圖片,不現實的不載入,於是我也仿照做了一個。我是菜鳥,我承認 呵呵,雖然不見得完全和他們的一樣,但是確實解決了翻頁時那一刻的卡頓現象。
因為未發現網上有相關文章,希望對朋友們有用~
下面是相關代碼(分頁的就沒放):
Java代碼
- /**
- * list滾動監聽
- */
- 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;
- }
- }
- });
/** * list滾動監聽 */listView.setOnScrollListener(new OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {// TODO Auto-generated method stub// 非同步載入圖片if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {//list停止滾動時載入圖片pageImgLoad(_start_index, _end_index);}}@Overridepublic 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;}}});
Java代碼
- /**
- * 只載入from start_index to end_index 的圖片
- * @param start_index
- * @param end_index
- */
- private void pageImgLoad(int start_index,
int end_index) {
- for (; start_index < end_index; start_index++) {
- HashMap<String, Object> curr_item = adapter.getItem(start_index);
- if (curr_item.get(Constant.NEWS_ICON_URL) !=
null
- && curr_item.get(Constant.NEWS_ICON) == null) {
- loadImage(curr_item);
- }
- }
- }
/** * 只載入from start_index to end_index 的圖片 * @param start_index * @param end_index */private void pageImgLoad(int start_index, int end_index) {for (; start_index < end_index; start_index++) {HashMap<String, Object> curr_item = adapter.getItem(start_index);if (curr_item.get(Constant.NEWS_ICON_URL) != null&& curr_item.get(Constant.NEWS_ICON) == null) {loadImage(curr_item);}}}
非同步載入圖片代碼,這裡我之前使用的是AsyncTask,但是繼承AsyncTask後不能被執行多次,所以我改用了線程呼叫handler更新UI:
Java代碼
- /**
- * 非同步載入圖片
- * @param curr_item
- */
- private void loadImage(final HashMap<String, Object> curr_item) {
- executorService.submit(new Runnable() {
- public void run() {
- try {
- Drawable curr_icon = null;
- String icon_URL = (String) curr_item
- .get(Constant.NEWS_ICON_URL);
- String newsId = (String) curr_item.get(Constant.NEWS_ID);
- if (imageCache.containsKey(icon_URL)) {//軟引用
- SoftReference<Drawable> softReference = imageCache
- .get(icon_URL);
- curr_icon = softReference.get();
- System.out.println("CASE USING SoftReference!!!!!!!!!!!!!!!!!!!!");
- }
- if (curr_icon == null) {
- HttpUtils hu = new HttpUtils();
- FileUtils fu = new FileUtils();
- if (hu.is_Intent(Home_Activity.this)) {
- fu.write2LocalFromIS(Home_Activity.this, newsId
- + Constant.SAVE_NEWS_ICON_NAME
- + Constant.SAVE_IMG_SUFFIX,
- hu.getISFromURL(icon_URL));
- }
- // 從本地載入圖片 如果沒網則直接載入本地圖片
- curr_icon = fu.readDrawableFromLocal(
- Home_Activity.this, newsId
- + Constant.SAVE_NEWS_ICON_NAME
- + Constant.SAVE_IMG_SUFFIX);
- imageCache.put(icon_URL, new SoftReference<Drawable>(
- curr_icon));
- }
- curr_item.put(Constant.NEWS_ICON, curr_icon);
- // UI交給handler更新
- Message msg = _viewHandler.obtainMessage();
- msg.arg1 = Constant.MSG_LIST_IMG_OK;
- msg.sendToTarget();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- });
- }
/** * 非同步載入圖片 * @param curr_item */private void loadImage(final HashMap<String, Object> curr_item) {executorService.submit(new Runnable() {public void run() {try {Drawable curr_icon = null;String icon_URL = (String) curr_item.get(Constant.NEWS_ICON_URL);String newsId = (String) curr_item.get(Constant.NEWS_ID);if (imageCache.containsKey(icon_URL)) {//軟引用SoftReference<Drawable> softReference = imageCache.get(icon_URL);curr_icon = softReference.get();System.out.println("CASE USING SoftReference!!!!!!!!!!!!!!!!!!!!");}if (curr_icon == null) {HttpUtils hu = new HttpUtils();FileUtils fu = new FileUtils();if (hu.is_Intent(Home_Activity.this)) {fu.write2LocalFromIS(Home_Activity.this, newsId+ Constant.SAVE_NEWS_ICON_NAME+ Constant.SAVE_IMG_SUFFIX,hu.getISFromURL(icon_URL));}// 從本地載入圖片 如果沒網則直接載入本地圖片curr_icon = fu.readDrawableFromLocal(Home_Activity.this, newsId+ Constant.SAVE_NEWS_ICON_NAME+ Constant.SAVE_IMG_SUFFIX);imageCache.put(icon_URL, new SoftReference<Drawable>(curr_icon));}curr_item.put(Constant.NEWS_ICON, curr_icon);// UI交給handler更新Message msg = _viewHandler.obtainMessage();msg.arg1 = Constant.MSG_LIST_IMG_OK;msg.sendToTarget();} catch (Exception e) {throw new RuntimeException(e);}}});}
Java代碼
- handler代碼:
handler代碼:
Java代碼
- Handler _viewHandler = new Handler() {
Handler _viewHandler = new Handler() {
Java代碼
- @Override
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case Constant.MSG_LIST_IMG_OK:
- // 更新UI
- adapter.notifyDataSetChanged();
- break;
- }
- super.handleMessage(msg);
- }
- };
@Overridepublic void handleMessage(Message msg) {switch (msg.arg1) {case Constant.MSG_LIST_IMG_OK:// 更新UIadapter.notifyDataSetChanged();break;}super.handleMessage(msg);}};
上個圖吧: