最近在做ListView分页显示,其中包括图片 和文字(先下载解析文字内容,再异步加载图片)发现每次点击下一页后,文字内容加载完毕,马上向下滑动,由于这时后台在用线程池异步下载图片,我每页有20条,也就是20张图片,会导致listview滑动卡顿!

这是用户不想看到的,我参考了网易新闻和电子市场等应用,发现它们都是只加载屏幕内的图片,不现实的不加载,于是我也仿照做了一个。我是菜鸟,我承认 呵呵,虽然不见得完全和他们的一样,但是确实解决了翻页时那一刻的卡顿现象。

因为未发现网上有相关文章,希望对朋友们有用~

下面是相关代码(分页的就没放):

  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. });
  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. });
  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. }
  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:

  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. }
  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. }
  1. handler代码:
  1. handler代码:
  1. Handler _viewHandler = new Handler() {
  1. Handler _viewHandler = new Handler() {
  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. };
  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. };

上个图吧:

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

Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)的更多相关文章

  1. Android RecyclerView使用 及 滑动时加载图片优化方案

    1.控制线程数量 + 数据分页加载2.重写onScrollStateChanged方法 这个我们后面再谈,下面先来看看RecyclerView控件的使用及我们为什么选择使用它 RecyclerView ...

  2. json解析,异步下载(listview仅滑动时加载)Demo总结

    异步加载的练习demo 主要涉及知识点: 1.解析json格式数据,主要包括图片,文本 2.使用AsynTask异步方式从网络下载图片 3.BaseAdapter的"优雅"使用 4 ...

  3. Listview滑动时不加载数据,停下来时加载数据,让App更优

    http://blog.csdn.net/yy1300326388/article/details/45153813

  4. android ListView中button点击事件盖掉onItemClick解决办法

    ListView 1.在android应用当中,很多时候都要用到listView,但如果ListView当中添加Button后,ListView 自己的 public void onItemClick ...

  5. android listview使用自定义的adapter没有了OnItemClickListener事件解决办法

    在使用listview的时用使用自定义的adapter的时候,如果你的item布局中包含有Button,Checkable继承来的所有控件,那么你将无法获取listview的onItemClickLi ...

  6. Android批量图片加载经典系列——使用LruCache、AsyncTask缓存并异步加载图片

    一.问题描述 使用LruCache.AsyncTask实现批量图片的加载并达到下列技术要求 1.从缓存中读取图片,若不在缓存中,则开启异步线程(AsyncTask)加载图片,并放入缓存中 2.及时移除 ...

  7. 提升Android ListView性能的几个技巧

    ListView如何运作的? ListView是设计应用于对可扩展性和高性能要求的地方.实际上,这就意味着ListView有以下2个要求: 尽可能少的创建View: 只是绘制和布局在屏幕上可见的子Vi ...

  8. ios UIWebView加载HTMLStr图文,关于图片宽高设置,webView内容实际高度的踩坑问题

    一.关于UIWebView 与 WKWebView 选取问题 从发布时间看: 2008年7月11日,在新一代iPhone3G正式发售当天,iPhone OS 2.0(iOS 2.0)推出,这时候就有U ...

  9. 图片利用 new Image()预加载原理 和懒加载的实现原理

    二:预加载和懒加载的区别 预加载与懒加载,我们经常经常用到,这些技术不仅仅限于图片加载,我们今天讨论的是图片加载: 图片预加载:顾名思义,图片预加载就是在网页全部加载之前,提前加载图片.当用户需要查看 ...

随机推荐

  1. WebService服务发布与使用(JDK自带WebService)

    简单粗暴,直接上步骤 一.先建立一个web项目,名字叫MyService 名字为MyService 新建Java类 package com.webService; import javax.jws.W ...

  2. BTrace使用简介

    很多时候在online的应用出现问题时,很多时候我们需要知道更多的程序的运行细节,但又不可能在开发的时候就把程序中所有的运行细节都打印到日志上,通常这个时候能采取的就是修改代码,重新部署,然后再观察, ...

  3. Image Processing in Python with Pillow

    Introduction A lot of applications use digital images, and with this there is usually a need to proc ...

  4. 使用Python登录Github网站

    在下面的代码中, 展示了使用Python脚本登录Github的方法. 如果需要登录别的网站,那么请使用Chrome的Inspect的功能寻找到目标的object,对代码进行替换. 代码先登录了gith ...

  5. Android + Eclipse + PhoneGap 2.9.0 安卓最新环境配置,部分资料整合网上资料,已成功安装.

    前言:最近心血来潮做了一个以品牌为中心的网站,打算推出本地服务o2o应用.快速开发手机应用,最后选择了phonegap,这里我只是讲述我安装的过程,仅供大家参考. 我开发的一个模型http://www ...

  6. Emoji 编码

    https://segmentfault.com/a/1190000007594620 http://cenalulu.github.io/linux/character-encoding/ http ...

  7. 最新整合maven+SSM+Tomcat 实现注册登录

    mybatis学习 http://www.mybatis.org/mybatis-3/zh/index.html Spring学习:http://blog.csdn.net/king1425/arti ...

  8. [转]POJ3624 Charm Bracelet(典型01背包问题)

    来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...

  9. swift常用第三方库

    网络 Alamofire:http网络请求事件处理的框架. Moya:这是一个基于Alamofire的更高层网络请求封装抽象层. Reachability.swift:用来检查应用当前的网络连接状况. ...

  10. springboot本地读取resources/images没问题,上传到云服务器打成jar包就读取不到问题

    //String watermarkfileName = this.getClass().getClassLoader().getResource("images/watermark.png ...