一般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的性能小结的更多相关文章

  1. 转——Android应用开发性能优化完全分析

    [工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.] 1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉 ...

  2. Android 应用开发性能优化完全分析

    1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉大家你一总结.我一总结的都说到了很多优化注意事项,但是看过这些文章后大多数存在一个问题就是只 ...

  3. 【转】Android应用开发性能优化完全分析

    http://blog.csdn.net/yanbober/article/details/48394201 1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关 ...

  4. Android应用开发性能优化完全分析

    1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉大家你一总结.我一总结的都说到了很多优化注意事项,但是看过这些文章后大多数存在一个问题就是只 ...

  5. ym——Android之ListView性能优化

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! Android之ListView性能优化 假设有看过我写过的15k面试题的朋友们一定知 ...

  6. 转:Android应用开发性能优化完全分析

    转自:http://blog.csdn.net/yanbober/article/details/48394201 1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜 ...

  7. Android JNI&NDK编程小结及建议

    前言 由于网上关于JNI/NDK相关的知识点介绍的比较零散而且不具备参照性,所以写了这篇JNI/NDK笔记,便于作为随时查阅的工具类型的文章,本文主要的介绍了在平时项目中常用的命令.JNI数据类型.签 ...

  8. Android 即时通讯开发小结(一)

    <Android 即时通讯开发小结>基于IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对IM 开发做一个全面的总结. 相关推荐阅读:. Android 即时通讯 ...

  9. Ionic中使用Chart.js进行图表展示以及在iOS/Android中的性能差异

    Angular Chart 简介 在之前的文章中介绍了使用 Ionic 开发跨平台(iOS & Android)应用中遇到的一些问题的解决方案. 在更新0.1.3版本的过程中遇到了需要使用图表 ...

随机推荐

  1. 【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符

    1.问题描述 最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作.因此编写了自定义的Lint规则,在调试过程中,编译的时候出现了如下所示的错误: 部分输出日志如下所示: ...

  2. EOS Dapp开发(1)-基于Docker的开发环境搭建

    随着EOS主网的上线,相信基于EOS的Dapp开发会越来越多,查阅了很多资料相关的开发资料都不是很多,只能自己摸索,按照网上仅有的几篇教程,先git clonehttps://github.com/E ...

  3. 第1章 1.10计算机网络概述--OSI参考模型和TCP_IP协议

    传输层负责将大数据文件分段,变成数据段. 网络层负责为小分段加上IP地址,变成数据包. 数据链路层负责将数包加上MAC地址和校验值,变成数据帧. TCP/IP协议是一群协议.不只是2个协议.

  4. sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误。

    sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误. 错误情况,如下图: 原因分析:硬盘故障 解决办法:进行迁移

  5. Django model中数据批量导入bulk_create()

    在Django中需要向数据库中插入多条数据(list).使用如下方法,每次save()的时候都会访问一次数据库.导致性能问题: for i in resultlist: p = Account(nam ...

  6. html结构和标签

    <!DOCTYPE html><meta charset="utf-8"><header>表示页面的一个内容区块,或整个页面的标题</he ...

  7. KVM入门

    KVM KVM(Kernel-based Virtual Machine)是众多虚拟化技术之一,它是Linux内核中的一个模块,该模块依赖于CPU,如果CPU支持虚拟化,那么该模块才可以被加载.KVM ...

  8. stm32_CAN总线知识(转)

    源: stm32_CAN总线知识

  9. Django:表结构发生变化需要执行命令

    Django:表结构发生变化需要执行命令 Django:表结构发生变化需要执行命令 mysite> python manage.py makemigrations blog #让 Django ...

  10. AJAX,JSON,GSON

    AJAX将数据使用JSON格式发送给后端Servlet或其他语言解析. 对JSON内容使用GSON外扩展包进行分解,并使用(如查询用户名是否已经被注册), 最后使用Map集合设置新的返回状态码,并使用 ...