Android ListView只加載當前屏幕內的圖片(解決list滑動時加載卡頓)

最近在作ListView分頁顯示,其中包括圖片 和文字(先下載解析文字內容,再異步加載圖片)發現每次點擊下一頁後,文字內容加載完畢,立刻向下滑動,因爲這時後臺在用線程池異步下載圖片,我每頁有20條,也就是20張圖片,會致使listview滑動卡頓!java

這是用戶不想看到的,我參考了網易新聞和電子市場等應用,發現它們都是隻加載屏幕內的圖片,不現實的不加載,因而我也仿照作了一個。我是菜鳥,我認可 呵呵,雖然不見得徹底和他們的同樣,可是確實解決了翻頁時那一刻的卡頓現象。android

由於未發現網上有相關文章,但願對朋友們有用~異步

下面是相關代碼(分頁的就沒放):ide

Java代碼 複製代碼 收藏代碼
  1. /**
  2. * list滾動監聽
  3. */
  4. listView.setOnScrollListener(new OnScrollListener() {
  5. @Override
  6. public void onScrollStateChanged(AbsListView view, int scrollState) {
  7. // TODO Auto-generated method stub
  8. // 異步加載圖片
  9. if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {//list中止滾動時加載圖片
  10. pageImgLoad(_start_index, _end_index);
  11. }
  12. }
  13. @Override
  14. public void onScroll(AbsListView view, int firstVisibleItem,
  15. int visibleItemCount, int totalItemCount) {
  16. // TODO Auto-generated method stub
  17. //設置當前屏幕顯示的起始index和結束index
  18. _start_index = firstVisibleItem;
  19. _end_index = firstVisibleItem + visibleItemCount;
  20. if (_end_index >= totalItemCount) {
  21. _end_index = totalItemCount - 1;
  22. }
  23. }
  24. });
[java] view plain copy
  1. /** 
  2.      * list滾動監聽 
  3.      */  
  4.     listView.setOnScrollListener(new OnScrollListener() {  
  5.         @Override  
  6.         public void onScrollStateChanged(AbsListView view, int scrollState) {  
  7.             // TODO Auto-generated method stub  
  8.             // 異步加載圖片  
  9.             if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {//list中止滾動時加載圖片  
  10.                 pageImgLoad(_start_index, _end_index);  
  11.             }  
  12.         }  
  13.         @Override  
  14.         public void onScroll(AbsListView view, int firstVisibleItem,  
  15.                 int visibleItemCount, int totalItemCount) {  
  16.             // TODO Auto-generated method stub  
  17.             //設置當前屏幕顯示的起始index和結束index  
  18.             _start_index = firstVisibleItem;  
  19.             _end_index = firstVisibleItem + visibleItemCount;  
  20.             if (_end_index >= totalItemCount) {  
  21.                 _end_index = totalItemCount - 1;  
  22.             }  
  23.         }  
  24.     });  
Java代碼 複製代碼 收藏代碼
  1. /**
  2. * 只加載from start_index to end_index 的圖片
  3. * @param start_index
  4. * @param end_index
  5. */
  6. private void pageImgLoad(int start_index, int end_index) {
  7. for (; start_index < end_index; start_index++) {
  8. HashMap<String, Object> curr_item = adapter.getItem(start_index);
  9. if (curr_item.get(Constant.NEWS_ICON_URL) != null
  10. && curr_item.get(Constant.NEWS_ICON) == null) {
  11. loadImage(curr_item);
  12. }
  13. }
  14. }
[java] view plain copy
  1. /** 
  2.      * 只加載from start_index to end_index 的圖片  
  3.      * @param start_index 
  4.      * @param end_index 
  5.      */  
  6.     private void pageImgLoad(int start_index, int end_index) {  
  7.         for (; start_index < end_index; start_index++) {  
  8.             HashMap<String, Object> curr_item = adapter.getItem(start_index);  
  9.             if (curr_item.get(Constant.NEWS_ICON_URL) != null  
  10.                     && curr_item.get(Constant.NEWS_ICON) == null) {  
  11.                 loadImage(curr_item);  
  12.             }  
  13.         }  
  14.     }  

 

異步加載圖片代碼,這裏我以前使用的是AsyncTask,可是繼承AsyncTask後不能被執行屢次,因此我改用了線程呼叫handler更新UI:this

Java代碼 複製代碼 收藏代碼
  1. /**
  2. * 異步加載圖片
  3. * @param curr_item
  4. */
  5. private void loadImage(final HashMap<String, Object> curr_item) {
  6. executorService.submit(new Runnable() {
  7. public void run() {
  8. try {
  9. Drawable curr_icon = null;
  10. String icon_URL = (String) curr_item
  11. .get(Constant.NEWS_ICON_URL);
  12. String newsId = (String) curr_item.get(Constant.NEWS_ID);
  13. if (imageCache.containsKey(icon_URL)) {//軟引用
  14. SoftReference<Drawable> softReference = imageCache
  15. .get(icon_URL);
  16. curr_icon = softReference.get();
  17. System.out.println("CASE USING SoftReference!!!!!!!!!!!!!!!!!!!!");
  18. }
  19. if (curr_icon == null) {
  20. HttpUtils hu = new HttpUtils();
  21. FileUtils fu = new FileUtils();
  22. if (hu.is_Intent(Home_Activity.this)) {
  23. fu.write2LocalFromIS(Home_Activity.this, newsId
  24. + Constant.SAVE_NEWS_ICON_NAME
  25. + Constant.SAVE_IMG_SUFFIX,
  26. hu.getISFromURL(icon_URL));
  27. }
  28. // 從本地加載圖片 若是沒網則直接加載本地圖片
  29. curr_icon = fu.readDrawableFromLocal(
  30. Home_Activity.this, newsId
  31. + Constant.SAVE_NEWS_ICON_NAME
  32. + Constant.SAVE_IMG_SUFFIX);
  33. imageCache.put(icon_URL, new SoftReference<Drawable>(
  34. curr_icon));
  35. }
  36. curr_item.put(Constant.NEWS_ICON, curr_icon);
  37. // UI交給handler更新
  38. Message msg = _viewHandler.obtainMessage();
  39. msg.arg1 = Constant.MSG_LIST_IMG_OK;
  40. msg.sendToTarget();
  41. } catch (Exception e) {
  42. throw new RuntimeException(e);
  43. }
  44. }
  45. });
  46. }
[java] view plain copy
  1. /** 
  2.      * 異步加載圖片 
  3.      * @param curr_item 
  4.      */  
  5.     private void loadImage(final HashMap<String, Object> curr_item) {  
  6.         executorService.submit(new Runnable() {  
  7.             public void run() {  
  8.                 try {  
  9.                     Drawable curr_icon = null;  
  10.                     String icon_URL = (String) curr_item  
  11.                             .get(Constant.NEWS_ICON_URL);  
  12.                     String newsId = (String) curr_item.get(Constant.NEWS_ID);  
  13.   
  14.                     if (imageCache.containsKey(icon_URL)) {//軟引用  
  15.                         SoftReference<Drawable> softReference = imageCache  
  16.                                 .get(icon_URL);  
  17.                         curr_icon = softReference.get();  
  18.                         System.out.println("CASE USING SoftReference!!!!!!!!!!!!!!!!!!!!");  
  19.                     }  
  20.                     if (curr_icon == null) {  
  21.                         HttpUtils hu = new HttpUtils();  
  22.                         FileUtils fu = new FileUtils();  
  23.                         if (hu.is_Intent(Home_Activity.this)) {  
  24.                             fu.write2LocalFromIS(Home_Activity.this, newsId  
  25.                                     + Constant.SAVE_NEWS_ICON_NAME  
  26.                                     + Constant.SAVE_IMG_SUFFIX,  
  27.                                     hu.getISFromURL(icon_URL));  
  28.                         }  
  29.                         // 從本地加載圖片 若是沒網則直接加載本地圖片  
  30.                         curr_icon = fu.readDrawableFromLocal(  
  31.                                 Home_Activity.this, newsId  
  32.                                         + Constant.SAVE_NEWS_ICON_NAME  
  33.                                         + Constant.SAVE_IMG_SUFFIX);  
  34.                         imageCache.put(icon_URL, new SoftReference<Drawable>(  
  35.                                 curr_icon));  
  36.                     }  
  37.                     curr_item.put(Constant.NEWS_ICON, curr_icon);  
  38.                     // UI交給handler更新  
  39.                     Message msg = _viewHandler.obtainMessage();  
  40.                     msg.arg1 = Constant.MSG_LIST_IMG_OK;  
  41.                     msg.sendToTarget();  
  42.                 } catch (Exception e) {  
  43.                     throw new RuntimeException(e);  
  44.                 }  
  45.             }  
  46.         });  
  47.     }  
Java代碼 複製代碼 收藏代碼
  1. handler代碼:
[java] view plain copy
  1. handler代碼:  
Java代碼 複製代碼 收藏代碼
  1. Handler _viewHandler = new Handler() {
[java] view plain copy
  1. Handler _viewHandler = new Handler() {  
Java代碼 複製代碼 收藏代碼
  1. @Override
  2. public void handleMessage(Message msg) {
  3. switch (msg.arg1) {
  4. case Constant.MSG_LIST_IMG_OK:
  5. // 更新UI
  6. adapter.notifyDataSetChanged();
  7. break;
  8. }
  9. super.handleMessage(msg);
  10. }
  11. };
[java] view plain copy
  1. @Override  
  2.     public void handleMessage(Message msg) {  
  3.         switch (msg.arg1) {  
  4.         case Constant.MSG_LIST_IMG_OK:  
  5.             // 更新UI  
  6.             adapter.notifyDataSetChanged();  
  7.             break;  
  8.         }  
  9.         super.handleMessage(msg);  
  10.     }  
  11. };  

上個圖吧:spa


轉自:http://blog.csdn.net/fengkuanghun/article/details/6922131.net

相關文章
相關標籤/搜索