android listview 只加载显示的图片大小,Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)...
最近在做ListView分页显示,其中包括图片 和文字(先下载解析文字内容,再异步加载图片)发现每次点击下一页后,文字内容加载完毕,马上向下滑动,由于这时后台在用线程池异步下载图片,我每页有20条,也就是20张图片,会导致listview滑动卡顿!
这是用户不想看到的,我参考了网易新闻和电子市场等应用,发现它们都是只加载屏幕内的图片,不现实的不加载,于是我也仿照做了一个。我是菜鸟,我承认 呵呵,虽然不见得完全和他们的一样,但是确实解决了翻页时那一刻的卡顿现象。
因为未发现网上有相关文章,希望对朋友们有用~
下面是相关代码(分页的就没放):
Java代码
/**
* 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;
}
}
});
Java代码
/**
* 只加载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 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
HashMap 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:
Java代码
/**
* 异步加载图片
* @param curr_item
*/
private void loadImage(final HashMap 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 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(
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 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 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(
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);
}
}
});
}
Java代码
handler代码:handler代码:
Java代码
Handler _viewHandler = new Handler() {Handler _viewHandler = new Handler() {
Java代码
@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);
}
};
android listview 只加载显示的图片大小,Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)...相关推荐
- Android RecyclerView使用 及 滑动时加载图片优化方案
1.控制线程数量 + 数据分页加载2.重写onScrollStateChanged方法 这个我们后面再谈,下面先来看看RecyclerView控件的使用及我们为什么选择使用它 RecyclerView ...
- RecyclerView使用 及 滑动时加载图片优化方案
RecyclerView使用 及 滑动时加载图片优化方案 简述 本篇博文主要给大家分享关于RecyclerView控件的使用及通过继承RecyclerView来实现滑动时加载图片的优化方案,也同样能解 ...
- LIstview滑动时不加载图片,停止时加载!
//参照 http://blog.csdn.net/yy1300326388/article/details/45153813 public class CarWashDistanceAdapter ...
- android页面设置背景图片大小,android页面设置background为图片后,页面滑动掉帧问题...
最近接手的一个android项目里面,有个viewpager+3个fragment的页面,就是很常见的可以左右滑动切换页面的那种布局.接手的时候告诉我,这个页面有卡顿现象,性能需要优化.一开始觉得是f ...
- 随笔:说说第一次在android中嵌入非全屏显示的unity游戏时的坑之——界面切换时出现延迟/卡顿/花屏等现象解决方法
One 最近对接了个unity3D做的小游戏到android项目中:游戏嵌入项目的界面且非全屏显示,项目几经波折,到我手上的时候,已经采用了在同一个activity中用两个View来分别显示andro ...
- android html 字体颜色代码,Android TextView通过解析html显示不同颜色和大小
先贴一张效果图 效果 介绍 通过SpannableString.SpannableStringBuilder可以很方便的给TextView加上各种各样的样式,比如不同的颜色和大小,这里就不多说了,具体 ...
- android相册和拍照并裁剪图片大小,Android 拍照并对照片进行裁剪和压缩实例详解...
Android 拍照并对照片进行裁剪和压缩实例详解 本文主要介绍 Android 调用摄像头拍照并对照片进行裁剪和压缩,文中给出了主要步骤和关键代码. 调用摄像头拍照,对拍摄照片进行裁剪,代码如下. ...
- android+怎么分享图片大小,Android微信分享图片大于32k进行压缩
微信分享视频的时候,需要传一个图片数组,大小不能大于32k. 解决方案:使用Bitmap自带的compress方法解决了这个问题. 源码如下: package com.example.test; im ...
- Android开发之EditText输入显示文字hint大小设置
我们先来看下图: 这xml预览图 我们再来看下效果图: 我们来看下如何做到的: 很简单直接上代码 String hintStr = "输入时长";SpannableString s ...
最新文章
- 安装scala之后,命令行中输入scala报错nullpointException
- vim中开shell
- function_core.php is missing下载,discuz中 function_core.php中的dmkdir有死环bug
- Linux安装phpMyAdmin详细步骤
- Scala闭包特性的一个测试
- 蓝桥杯第七届决赛JAVA真题----路径之谜
- 对6月份的项目的总结
- 一个普通handler会持有activity引用吗_详解handler机制
- linux网络编程应用于生活,[Linux网络编程]应用实例--获取网络时间
- WSL配置C/D盘大小写敏感
- java Android SDK安装与环境变量配置以及开发第一个Android程序
- 机器学习Scikit-Learn基本操作实战
- en55032最新标准下载_施工升降机事故案例ppt(可下载)
- mysql emoji表情_mysql utf8mb4与emoji表情
- Web UI设计基础
- Eigen教程3----矩阵、向量以及标量的运算,转置、共轭以及伴随矩阵
- MODULE_DEVICE_TABLE宏的作用
- android模拟qq进场动画,Android用ViewPager仿QQ实现多页面滑动及动画效果
- 还记得愤怒的小鸟嘛?今天用Python给大家安排一波!
- java实现打印金字塔
热门文章
- NLP --- 隐马尔可夫HMM(第三个问题详解及HMM的应用)
- python深度复制_Python直接赋值、浅拷贝和深度拷贝解析
- 在HTML中标记tel是什么意思,'tel'属性在AEM 6.1的经典UI中富文本编辑器中的锚标记的href中不起作用...
- Docker+SVN
- 小程序开发 js里面array操作的方法列表。
- EF里查看/修改实体的当前值、原始值和数据库值
- IOS程序之发送短信代码实现
- 【工程处理技巧一篇】基于半规则数据的命名实体消歧识别【未完】
- matlab 画非线性曲线,matlab 非线性曲线拟合, nlinfit lsqcurvefit lsqnonlin
- mysql多数据源事务_springboot项目多数据源及其事务