android adapter的性能小结

摘要:
它完全在本地加载。效果比以前好得多,但它仍然被卡住了。最后,采用最朴实的方法。添加时,直接创建一个新视图,然后将整个视图放入缓存。

一般adapter的做法会重写getView方法

比如

1 @Override
2     public View getView(intposition, View convertView, ViewGroup parent) {
3         if (convertView == null) {
4             convertView = LayoutInflater.from(context).inflate(R.layout.contentitem, null);
5 }
6         TextView title =(TextView) convertView.findViewById(R.id.textViewTitle);
7         TextView author =(TextView) convertView.findViewById(R.id.textViewAuthor);
8         TextView content =(TextView) convertView.findViewById(R.id.textViewContent);
9         TextView otherInfo =(TextView) convertView.findViewById(R.id.textViewOtherInfo);
10         ImageView contentImage =(ImageView)convertView.findViewById(R.id.imageView);
11         ContentInfo info =data.get(position);
12 title.setText(info.title);
13 author.setText(info.author);
14 content.setText(info.content);
15 otherInfo.setText(info.otherInfo);
16         newHttpImageLoader(contentImage).load(info.imageUri);
17         convertView.setLayoutParams(newAbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
18         returnconvertView;
19     }

这样写有一个问题,就是如果我的图片比较大,contentImage 的加载时间就会比较长,那么当你很快的滚动listview的时候,就会刷新不过来。

为此我做了这样一个缓存

1 public View getView(intposition, View convertView, ViewGroup parent) {
2         if (convertView == null) {
3             convertView = LayoutInflater.from(context).inflate(R.layout.contentitem, null);
4 }
5 
6         TextView title =(TextView) convertView.findViewById(R.id.textViewTitle);
7         TextView author =(TextView) convertView.findViewById(R.id.textViewAuthor);
8         TextView content =(TextView) convertView.findViewById(R.id.textViewContent);
9         TextView otherInfo =(TextView) convertView.findViewById(R.id.textViewOtherInfo);
10         ImageView contentImage =(ImageView)convertView.findViewById(R.id.imageView);
11         ContentInfo info =data.get(position);
12 title.setText(info.title);
13 author.setText(info.author);
14 content.setText(info.content);
15 otherInfo.setText(info.otherInfo);
16         newHttpImageLoader(contentImage).load(info.imageUri);
17         convertView.setLayoutParams(newAbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
18 
19         returnconvertView;
20 }
21 
22     private classHttpImageLoader{
23         privateBitmap bitmap;
24         privateImageView image;
25 
26         final android.os.Handler handler = newandroid.os.Handler() {
27 @Override
28             public voidhandleMessage(Message msg) {
29                 super.handleMessage(msg);
30 image.setImageBitmap(bitmap);
31 }
32 };
33 
34         publicHttpImageLoader(ImageView view){
35             image =view;
36 }
37         public voidload(String url){
38             final String u =url;
39             if(map.containsKey(url)){
40 image.setImageBitmap(map.get(url));
41                 return;
42 }
43             newThread() {
44 @Override
45                 public voidrun() {
46                     bitmap =HttpUtil.getHttpBitmap(u);
47 map.put(u,bitmap);
48                     handler.sendEmptyMessage(0);
49 }
50 }.start();
51 
52 }
53     }
HttpImageLoader类中,每次加载一个图片就会将这个图片缓存起来放入到map中,这样省去了重新从网络读取的时间。完全是从本地加载。
效果比之前好很多,但是还是会卡。
最后采用了最土的方法。
添加的时候,直接new一个view出来,然后将整个view放入到缓存中。
1     public void add(finalContentInfo info) {
2         ContentItemView contentItemView  = newContentItemView(context);
3 contentItemView.setContentInfo(info);
4         contentItemView.setLayoutParams(newAbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
5 
6         contentItemView.setOnClickListener(newView.OnClickListener() {
7 @Override
8             public voidonClick(View v) {
9                 Intent intent = new Intent(context,ArticleActivity.class);
10                 Bundle bundle = newBundle();
11                 intent.putExtra("info",info);
12 context.startActivity(intent);
13 }
14 });
15 data.add(contentItemView);
16     }

getView的时候直接从代码中将整个view取出来

1 @Override
2     public View getView(intposition, View convertView, ViewGroup parent) {
3         returndata.get(position);
4     }

这样虽然比较耗内存,但是整个会变得很流畅。

不过如果这样做的话,还不如直接用Scrollview+linearLayout的组合比较合适。

当然了,前提是我能够保证在listview中的item不会太多,内存的消耗能够在我的容忍范围之内,才可以这样做。

免责声明:文章转载自《android adapter的性能小结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇K8S系统学习(一)C++Vector使用方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信小程序 View:flex 布局

微信小程序 View 支持两种布局方式:Block 和 Flex 所有 View 默认都是 block 要使用 flex 布局的话需要显式的声明: display:flex; 下面就来介绍下微信小程序的 Flex 布局 先做一个简单的 demo <view class="main"><view class="item item1">...

亲身实践,JAVA最优良的Adapter模式适配器模式

网上关于JAVA的适配器模式例子有很多,但真的有少实在不怎么样,看也不懂。自己总结了一个在性能和结构上都很容易理解的Adapter模式。 Adapter模式也叫适配器模式,是由GoF提出的23种设计模式的一种。Adapter模式是构造型模式之一,通过Adapter模式,可以改变已有类(或外部类)的接口形式。 在大规模的系统开发过程中,我们常常碰到诸...

设计模式学习笔记十一:适配器模式(Adapter Pattern)

    1.概述    在软件开发中,我们经常会遇到系统间集成,在系统集成时,最常见的问题就是系统间的接口不一致。很多能够满足功能的系统模块,由于接口不一致,导致无法使用。例如,常用的媒体播放器是MS Media player和RealPlayer,他们的文件结构和软件接口完全不同,前者支持WMF格式的音频和视频,后者支持RM格式的音频和视频。如果我们希望自...

提高Interface Builder高效工作的8个技巧

转自“破船之家”的翻译文章,真的很不错的技巧,在此转发分享给大家。转自这里。 本文译自:8 Tips for working effectively with Interface Builder(需FQ) 先来看看目录: 介绍 使view的Size与view中的Content相适应  按住option键—观察所选中view与另外view边缘之间的距离 E...

android spinner学习

   用法 1 :以资源方式,静态展示 Spinner 选项 1.     在资源文件 (strings.xml) 中,增加:          < string name = "spin_prompt" > 请选择城市 </ string >          < string-array name = "cities" &...

Cesium动态绘制实体(点、标注、面、线、圆、矩形)

  //自定义绘制图形,支持 点,线,面,矩形,圆,标识,可自定义绘制过程中的和绘制完的预览 this.drawGraphic = function(view,_mode,_callback,_GraphicProperty){ //清空所有可能的监听和画到一半的图形 if(handler){...