android adapter的性能小结
2024-09-30 05:52:20
一般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不会太多,内存的消耗能够在我的容忍范围之内,才可以这样做。
最新文章
- theano sparse_block_dot
- DNS知识指南
- 《React Native入门与实战》读书笔记(1)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- JSP九大内置对象的作用和用法总结?
- XMPie部署与创建过程 - 快速指南
- cookie导读,理解什么是cookie
- github常见操作和常见错误!错误提示:fatal: remote origin already exists.
- CSS3系列之3D制作 再研究
- ASP.NET本质论第一章网站应用程序学习笔记2
- 修改mysql最大连接数的方法
- poj.2419.Forests (枚举 + set用法)
- Android view 的事件分发机制
- 最最基层的ajax交互代码jquery+java之间的json跨域传递以及java的json代码返回
- SVN linux端配置
- 【翻译】在Ext JS集成第三方库
- js中如何在不影响既有事件监听的前提下新增监听器
- ganglia监控架构
- Linux RPM、YUM、APT包管理工具
- input禁止输入空格
热门文章
- Day19 客户关系系统实战
- mysql 数据操作 单表查询 练习
- OleDb未指定错误
- Python中被双下划线包围的魔法方法
- linux C 程序内存布局
- 一次org.springframework.jdbc.BadSqlGrammarException ### Error querying database Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException问题排查过程
- ng-深度学习-课程笔记-7: 优化算法(Week2)
- Python的星号(*)和双星号(**)用法
- ListView的ScrollBar设置
- Python笔记 #05# Package &; pip3