android adapter的性能小结
一般adapter的做法会重写getView方法
比如
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contentitem, null);
}
TextView title = (TextView) convertView.findViewById(R.id.textViewTitle);
TextView author = (TextView) convertView.findViewById(R.id.textViewAuthor);
TextView content = (TextView) convertView.findViewById(R.id.textViewContent);
TextView otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
ImageView contentImage = (ImageView)convertView.findViewById(R.id.imageView);
ContentInfo info = data.get(position);
title.setText(info.title);
author.setText(info.author);
content.setText(info.content);
otherInfo.setText(info.otherInfo);
new HttpImageLoader(contentImage).load(info.imageUri);
convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
return convertView;
}
这样写有一个问题,就是如果我的图片比较大,contentImage 的加载时间就会比较长,那么当你很快的滚动listview的时候,就会刷新不过来。
为此我做了这样一个缓存
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contentitem, null);
}
TextView title = (TextView) convertView.findViewById(R.id.textViewTitle);
TextView author = (TextView) convertView.findViewById(R.id.textViewAuthor);
TextView content = (TextView) convertView.findViewById(R.id.textViewContent);
TextView otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
ImageView contentImage = (ImageView)convertView.findViewById(R.id.imageView);
ContentInfo info = data.get(position);
title.setText(info.title);
author.setText(info.author);
content.setText(info.content);
otherInfo.setText(info.otherInfo);
new HttpImageLoader(contentImage).load(info.imageUri);
convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
return convertView;
}
private class HttpImageLoader{
private Bitmap bitmap;
private ImageView image;
final android.os.Handler handler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
image.setImageBitmap(bitmap);
}
};
public HttpImageLoader(ImageView view){
image = view;
}
public void load(String url){
final String u = url;
if (map.containsKey(url)){
image.setImageBitmap(map.get(url));
return;
}
new Thread() {
@Override
public void run() {
bitmap = HttpUtil.getHttpBitmap(u);
map.put(u,bitmap);
handler.sendEmptyMessage(0);
}
}.start();
}
}
HttpImageLoader类中,每次加载一个图片就会将这个图片缓存起来放入到map中,这样省去了重新从网络读取的时间。完全是从本地加载。
效果比之前好很多,但是还是会卡。
最后采用了最土的方法。
添加的时候,直接new一个view出来,然后将整个view放入到缓存中。
public void add(final ContentInfo info) {
ContentItemView contentItemView = new ContentItemView(context);
contentItemView.setContentInfo(info);
contentItemView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
contentItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,ArticleActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("info",info);
context.startActivity(intent);
}
});
data.add(contentItemView);
}
getView的时候直接从代码中将整个view取出来
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return data.get(position);
}
这样虽然比较耗内存,但是整个会变得很流畅。
不过如果这样做的话,还不如直接用Scrollview+linearLayout的组合比较合适。
当然了,前提是我能够保证在listview中的item不会太多,内存的消耗能够在我的容忍范围之内,才可以这样做。
android adapter的性能小结的更多相关文章
- 转——Android应用开发性能优化完全分析
[工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.] 1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉 ...
- Android 应用开发性能优化完全分析
1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉大家你一总结.我一总结的都说到了很多优化注意事项,但是看过这些文章后大多数存在一个问题就是只 ...
- 【转】Android应用开发性能优化完全分析
http://blog.csdn.net/yanbober/article/details/48394201 1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关 ...
- Android应用开发性能优化完全分析
1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉大家你一总结.我一总结的都说到了很多优化注意事项,但是看过这些文章后大多数存在一个问题就是只 ...
- ym——Android之ListView性能优化
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! Android之ListView性能优化 假设有看过我写过的15k面试题的朋友们一定知 ...
- 转:Android应用开发性能优化完全分析
转自:http://blog.csdn.net/yanbober/article/details/48394201 1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜 ...
- Android JNI&NDK编程小结及建议
前言 由于网上关于JNI/NDK相关的知识点介绍的比较零散而且不具备参照性,所以写了这篇JNI/NDK笔记,便于作为随时查阅的工具类型的文章,本文主要的介绍了在平时项目中常用的命令.JNI数据类型.签 ...
- Android 即时通讯开发小结(一)
<Android 即时通讯开发小结>基于IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对IM 开发做一个全面的总结. 相关推荐阅读:. Android 即时通讯 ...
- Ionic中使用Chart.js进行图表展示以及在iOS/Android中的性能差异
Angular Chart 简介 在之前的文章中介绍了使用 Ionic 开发跨平台(iOS & Android)应用中遇到的一些问题的解决方案. 在更新0.1.3版本的过程中遇到了需要使用图表 ...
随机推荐
- centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
centos shell编程4[分发系统] 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要 ...
- centos LAMP第一部分-环境搭建 Linux软件删除方式,mysql安装,apache,PHP,apache和php结合,phpinfo页面,ldd命令 第十九节课
centos LAMP第一部分-环境搭建 Linux软件删除方式,mysql安装,apache,PHP,apache和php结合,phpinfo页面,ldd命令 第十九节课 打命令之后可以输入: e ...
- oracle 查询按月份分组
如下表table1: 日期(exportDate) 数量(amount) -------------- ----------- 14- ...
- 装饰器的修复wraps,偏函数partial 以及chain
将被装饰的函数的一些属性值赋值给 装饰器函数,最终让属性的显示更符合我们的直觉. from functools import wraps def wapper(func): @wraps(func) ...
- WCF For Silverlight跨域策略
在WCF的根目录下添加跨域文件 <?xml version="1.0" encoding="utf-8" ?> <access-policy& ...
- 39. Combination Sum(回溯)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 20154312曾林 - Exp1 PC平台逆向破解
1.逆向及Bof基础实践说明 1.1-实践目标 对象:pwn1(linux可执行文件) 目标:使程序执行另一个代码片段:getshell 内容: 手工修改可执行文件,改变程序执行流程,直接跳转到get ...
- MapReduce: number of mappers/reducers
14 down vote It's the other way round. Number of mappers is decided based on the number of splits. I ...
- 怎样在linux下对U盘进行格式化和分区
说明,为了不做无用功,首先必须卸载要分区的设备,分区才能执行成功.通过命令umount /media/?? 或者umount /mnt/??? 看你的实际情况,这一步必不可少.1.首先通过命令fdis ...
- table--边框样式设置
Table的一些设置(自适应以及溢出) table的两个属性 单行溢出点点显示 表格的宽度设置 双栏自适应连续连续英文符换行 1.table重置的两个属性: ①border-collapse: c ...