Make your Android Listview Fast and Efficient
2011-05-27 11:28
267 查看
Make your Android Listview Fast and Efficient
2010-06-21 18:32
2010-06-21 18:32
Android programming --- Make your Android Listview Fast and EfficientGoogle Staff有一篇Turbo-charge your UI关于加速和提高Android UI效率的文章。把其中关于ListView的部分整理了一下,如下。 1.Globle view ListView上面关联上需要现实的布局,控件等等。 Adapter建立数据和ListView上面表示控件的关系 DataSource就是存储的数据源,要在ListView表示的数据源 和iPhone中的View, ViewController,等基本上结构还是类似的 2.ListView 和Adpater建立关联和要注意的地方 * 对每一行的数据,对会调用 -- Adapter.getView()来返回要现实的数据。 * 返回一个New的View,系统开销还是比较大的,原文介绍是说Expensive * 数据量少,还好办,开销也不会太多,如果100万以上呢? 呵呵,这个数据 在Mobile上处理还是有点bt.... 目前测试到的数据10万左右很流程。 3.ListView每行表示的效率问题 不要这么干(Don't): 每次产生ListView的ListItem都会开销一个New View public View getView(int position, View convertView, ViewGroup parent) { View item = mInflater.inflate(R.layout.list_item_icon_text, null); ((TextView) item.findViewById(R.id.text)).setText(DATA[position]); ((ImageView) item.findViewById(R.id.icon)).setImageBitmap( (position & 1) == 1 ? mIcon1 : mIcon2); return item; } 可以这么干(Do): 判断试图时候为空,如果已经产生过了,就利用现在的。 public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.item, null); } ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]); ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap( (position & 1) == 1 ? mIcon1 : mIcon2); return convertView; } 最好这么做(Even better): 个人理解是做了一个Class,把需要展示的控件列入,加快了Memory clone的效率 static class ViewHolder { TextView text; ImageView icon; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_icon_text, null); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText(DATA[position]); holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); return convertView; 4.效率如何呢: 比较效果看后,利用ViewHolder后,速度大概有3倍以上的提升 http://leefj.spaces.live.com/blog/cns!ADF54DD0A549AD9B!450.trak |
相关文章推荐
- Make your Android Listview Fast and Efficient
- Google I/O 2009 Make your Android UI Fast and Efficient
- Android Run Time: An Easy Hack To Make Your Phone Faster And Its Battery Last Longer
- NullPointerException at android.widget.AbsListView.obtainView at android.widget.ListView.makeAndAddView
- [Android 問題] How to Add a Button in ListView, and Make the Event of onClick and onItemClick Coexist?
- Android ListView Fast and Smooth Scroll 終極秘技
- Android Build System ---- how to add your product and board config *.mk
- 自定义ListView FastScroller滑块图片 以及 android:fastScrollEnabled="true" 不起作用
- Android Build System ---- how to add your product and board config *.mk
- android开发游记:ScrollView嵌套ListView,ListView完全展开及makeMeasureSpec测量机制原理分析
- How to avoid common errors and make program efficient.
- unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make a
- Top 8 Programs That Can Make Your Computer Run really Fast
- Your content must have a ListView whose id attribute is 'android.R.id.list'
- Android-自定义ListView FastScroller滑块图片
- Script compiler android and make a file system
- Mac OS中, android stuido升级提示Connection failed. Please check your network connection and try again
- SPOJ 130 - Rent your airplane and make money(dp+优化)
- Android报错:Your content must have a ListView whose id attribute is 'android.R.id.list'
- Android listView 替换技术 RecyclerView And CardView