Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)
最近在做ListView分页显示,其中包括图片 和文字(先下载解析文字内容,再异步加载图片)发现每次点击下一页后,文字内容加载完毕,马上向下滑动,由于这时后台在用线程池异步下载图片,我每页有20条,也就是20张图片,会导致listview滑动卡顿!
这是用户不想看到的,我参考了网易新闻和电子市场等应用,发现它们都是只加载屏幕内的图片,不现实的不加载,于是我也仿照做了一个。我是菜鸟,我承认 呵呵,虽然不见得完全和他们的一样,但是确实解决了翻页时那一刻的卡顿现象。
因为未发现网上有相关文章,希望对朋友们有用~
下面是相关代码(分页的就没放):
- /**
- * 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() {
- @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;
- }
- }
- });
- /**
- * 只加载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:
- /**
- * 异步加载图片
- * @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);
- }
- }
- });
- }
- handler代码:
- handler代码:
- Handler _viewHandler = new Handler() {
- Handler _viewHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case Constant.MSG_LIST_IMG_OK:
- // 更新UI
- adapter.notifyDataSetChanged();
- break;
- }
- super.handleMessage(msg);
- }
- };
- @Override
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case Constant.MSG_LIST_IMG_OK:
- // 更新UI
- adapter.notifyDataSetChanged();
- break;
- }
- super.handleMessage(msg);
- }
- };
上个图吧:

转自:http://blog.csdn.net/fengkuanghun/article/details/6922131
Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)的更多相关文章
- Android RecyclerView使用 及 滑动时加载图片优化方案
1.控制线程数量 + 数据分页加载2.重写onScrollStateChanged方法 这个我们后面再谈,下面先来看看RecyclerView控件的使用及我们为什么选择使用它 RecyclerView ...
- json解析,异步下载(listview仅滑动时加载)Demo总结
异步加载的练习demo 主要涉及知识点: 1.解析json格式数据,主要包括图片,文本 2.使用AsynTask异步方式从网络下载图片 3.BaseAdapter的"优雅"使用 4 ...
- Listview滑动时不加载数据,停下来时加载数据,让App更优
http://blog.csdn.net/yy1300326388/article/details/45153813
- android ListView中button点击事件盖掉onItemClick解决办法
ListView 1.在android应用当中,很多时候都要用到listView,但如果ListView当中添加Button后,ListView 自己的 public void onItemClick ...
- android listview使用自定义的adapter没有了OnItemClickListener事件解决办法
在使用listview的时用使用自定义的adapter的时候,如果你的item布局中包含有Button,Checkable继承来的所有控件,那么你将无法获取listview的onItemClickLi ...
- Android批量图片加载经典系列——使用LruCache、AsyncTask缓存并异步加载图片
一.问题描述 使用LruCache.AsyncTask实现批量图片的加载并达到下列技术要求 1.从缓存中读取图片,若不在缓存中,则开启异步线程(AsyncTask)加载图片,并放入缓存中 2.及时移除 ...
- 提升Android ListView性能的几个技巧
ListView如何运作的? ListView是设计应用于对可扩展性和高性能要求的地方.实际上,这就意味着ListView有以下2个要求: 尽可能少的创建View: 只是绘制和布局在屏幕上可见的子Vi ...
- ios UIWebView加载HTMLStr图文,关于图片宽高设置,webView内容实际高度的踩坑问题
一.关于UIWebView 与 WKWebView 选取问题 从发布时间看: 2008年7月11日,在新一代iPhone3G正式发售当天,iPhone OS 2.0(iOS 2.0)推出,这时候就有U ...
- 图片利用 new Image()预加载原理 和懒加载的实现原理
二:预加载和懒加载的区别 预加载与懒加载,我们经常经常用到,这些技术不仅仅限于图片加载,我们今天讨论的是图片加载: 图片预加载:顾名思义,图片预加载就是在网页全部加载之前,提前加载图片.当用户需要查看 ...
随机推荐
- WebService服务发布与使用(JDK自带WebService)
简单粗暴,直接上步骤 一.先建立一个web项目,名字叫MyService 名字为MyService 新建Java类 package com.webService; import javax.jws.W ...
- BTrace使用简介
很多时候在online的应用出现问题时,很多时候我们需要知道更多的程序的运行细节,但又不可能在开发的时候就把程序中所有的运行细节都打印到日志上,通常这个时候能采取的就是修改代码,重新部署,然后再观察, ...
- Image Processing in Python with Pillow
Introduction A lot of applications use digital images, and with this there is usually a need to proc ...
- 使用Python登录Github网站
在下面的代码中, 展示了使用Python脚本登录Github的方法. 如果需要登录别的网站,那么请使用Chrome的Inspect的功能寻找到目标的object,对代码进行替换. 代码先登录了gith ...
- Android + Eclipse + PhoneGap 2.9.0 安卓最新环境配置,部分资料整合网上资料,已成功安装.
前言:最近心血来潮做了一个以品牌为中心的网站,打算推出本地服务o2o应用.快速开发手机应用,最后选择了phonegap,这里我只是讲述我安装的过程,仅供大家参考. 我开发的一个模型http://www ...
- Emoji 编码
https://segmentfault.com/a/1190000007594620 http://cenalulu.github.io/linux/character-encoding/ http ...
- 最新整合maven+SSM+Tomcat 实现注册登录
mybatis学习 http://www.mybatis.org/mybatis-3/zh/index.html Spring学习:http://blog.csdn.net/king1425/arti ...
- [转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...
- swift常用第三方库
网络 Alamofire:http网络请求事件处理的框架. Moya:这是一个基于Alamofire的更高层网络请求封装抽象层. Reachability.swift:用来检查应用当前的网络连接状况. ...
- springboot本地读取resources/images没问题,上传到云服务器打成jar包就读取不到问题
//String watermarkfileName = this.getClass().getClassLoader().getResource("images/watermark.png ...