最近在做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. redis源码分析

    我阅读的源码版本是redis-2.8.19 src目录下总共96个.h,.c文件 1. 数据结构相关源码(15个左右)字符串代码: sds.h, sds.c字典:dict.h, dict.c链表: a ...

  2. delphi Tdxribbon 设置背景和skin 皮肤

    https://blog.csdn.net/maxwoods/article/details/8592389 http://www.cnblogs.com/baoluo/archive/2011/04 ...

  3. App架构师实践指南六之性能优化三

    App架构师实践指南六之性能优化三 2018年08月02日 13:57:57 nicolelili1 阅读数:190   内存性能优化1.内存机制和原理 1.1 内存管理内存时一个基础又高深的话题,从 ...

  4. [转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取

      背景 记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的 ...

  5. 【BZOJ3585】mex

    Description 有一个长度为n的数组{a1,a2,-,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三行開始,每行一个询问l, ...

  6. Linux编译步骤概述

    Linux,一切皆文件! linux环境下,编译源码文件步骤总结 01.下载解压 一遍在开源网站有download/下载页面 02.安装基本编译环境 yum install -y gcc gcc-c+ ...

  7. PL/SQL出现存储过程注释中文乱码

    进入PL/SQL命令行窗口输入:select userenv('language') from dual 查出数据库字符集 输入:select * from V$NLS_PARAMETERS 查出NL ...

  8. 【SqlServer】Sqlserver中的DOS命令操作

    输入osql ?查看是否支持当前版本,如果是SQL Server 2005以上用Sqlcmd  ,  以下用Osql连接数据库(a)Osql -S localhost -U username -P p ...

  9. Adobe Photoshop for Mac(图像处理软件)破解版安装

    1.软件简介    Adobe Photoshop(简称 "PS")是 macOS 系统上一款由 Adobe Systems 开发和发行的图像处理软件.Photoshop 主要处理 ...

  10. 初始cfx开发webservice, 简单实例应用

    项目结构图: 步骤一: 添加maven 依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...