最近在作ListView分頁顯示,其中包括圖片 和文字(先下載解析文字內容,再異步加載圖片)發現每次點擊下一頁後,文字內容加載完畢,立刻向下滑動,因爲這時後臺在用線程池異步下載圖片,我每頁有20條,也就是20張圖片,會致使listview滑動卡頓!java
這是用戶不想看到的,我參考了網易新聞和電子市場等應用,發現它們都是隻加載屏幕內的圖片,不現實的不加載,因而我也仿照作了一個。我是菜鳥,我認可 呵呵,雖然不見得徹底和他們的同樣,可是確實解決了翻頁時那一刻的卡頓現象。android
由於未發現網上有相關文章,但願對朋友們有用~異步
下面是相關代碼(分頁的就沒放):ide
- listView.setOnScrollListener(new OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
- pageImgLoad(_start_index, _end_index);
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- _start_index = firstVisibleItem;
- _end_index = firstVisibleItem + visibleItemCount;
- if (_end_index >= totalItemCount) {
- _end_index = totalItemCount - 1;
- }
- }
- });
- listView.setOnScrollListener(new OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
-
-
- if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
- pageImgLoad(_start_index, _end_index);
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
-
-
- _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<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);
- }
- }
- }
- 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:this
- 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);
- Message msg = _viewHandler.obtainMessage();
- msg.arg1 = Constant.MSG_LIST_IMG_OK;
- msg.sendToTarget();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- });
- }
- 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);
-
- Message msg = _viewHandler.obtainMessage();
- msg.arg1 = Constant.MSG_LIST_IMG_OK;
- msg.sendToTarget();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- });
- }
- Handler _viewHandler = new Handler() {
- Handler _viewHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case Constant.MSG_LIST_IMG_OK:
- adapter.notifyDataSetChanged();
- break;
- }
- super.handleMessage(msg);
- }
- };
- @Override
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case Constant.MSG_LIST_IMG_OK:
-
- adapter.notifyDataSetChanged();
- break;
- }
- super.handleMessage(msg);
- }
- };
上個圖吧:spa
轉自:http://blog.csdn.net/fengkuanghun/article/details/6922131.net