提高显示布局文件的性能 4 - 提升ListView的性能
来源:互联网 发布:ubuntu和centos 编辑:程序博客网 时间:2024/06/06 21:55
Making ListView Scrolling Smooth [使得ListView滚动平滑]
使得滚动ListView平滑的关键在与保持AP的UI thread与复杂的操作隔离。
确保另起一个Thread来处理Disk IO,network access或者SQL access.
为了测试AP的状态,可以enable
为了测试AP的状态,可以enable
StrictMode
.(Android ICS 4.0上已经默认开启了StrickMode)Use a Background Thread [使用后台线程]
使用后台线程,这样可以使得UI线程可以专注于描绘UI。大多数时候,AsycnTask实现了一种简单把需要做的事情与main thread隔离的方法。
[关于如何使用AsyncTask,请参考官方详解,或者参看本人前面一篇文章:使用AsyncTask来处理一些简单的需要后台处理的动作]
下面是一个例子:
- // Using an AsyncTask to load the slow images in a background thread
- new AsyncTask<ViewHolder, Void, Bitmap>() {
- private ViewHolder v;
- @Override
- protected Bitmap doInBackground(ViewHolder... params) {
- v = params[0];
- return mFakeImageLoader.getImage();
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- super.onPostExecute(result);
- if (v.position == position) {
- // If this item hasn't been recycled already, hide the
- // progress and set and show the image
- v.progress.setVisibility(View.GONE);
- v.icon.setVisibility(View.VISIBLE);
- v.icon.setImageBitmap(result);
- }
- }
- }.execute(holder);
executeOnExecutor()
来替代execute(),这样系统会根据当前设备的内核数量同时进行多个任务。Hold View Objects in a View Holder [如何使用View Holder来Hold住view对象]
你的程序在滚动ListView的时候也许会重复频繁的call findViewById(),这样会降低性能。尽管Adapter会因为循环机制返回一个创建好的View(关于这个机制,请参考鄙人前面的文章:ListView中getView的原理与解决多轮重复调用的方法)
你仍然需要查找到这些组件并更新它,避免这样的重复,我们可以使用ViewHolder的设计模式。
A ViewHolder对象存放每一个View组件于Layout的tag属性中,因此我们可以立即访问tag中的组件从而避免重复call findViewById()。
A ViewHolder对象存放每一个View组件于Layout的tag属性中,因此我们可以立即访问tag中的组件从而避免重复call findViewById()。
下面是定义了一个ViewHolder的例子:
- static class ViewHolder {
- TextView text;
- TextView timestamp;
- ImageView icon;
- ProgressBar progress;
- int position;
- }
- ViewHolder holder = new ViewHolder();
- holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
- holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
- holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
- holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
- convertView.setTag(holder);
- 提高显示布局文件的性能 4 - 提升ListView的性能
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 4 - 使用ViewHolder提升ListView的性能]
- Android Training - 提升布局文件的性能(Lesson 4 - 使用ViewHolder来提升ListView的性能)
- 提高布局文件的显示性能。
- 提高显示布局文件的性能 1 - 优化布局层级
- 提高ListView的性能
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 1 - 优化布局层级]
- 提高ListView性能的技巧
- 提高显示布局文件的性能 3 - 按需载入视图(ViewStub的使用方法)
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 2 - 使用include标签重用Layout]
- 提高显示布局文件的性能 2 - 使用include标签重用Layout
- Android提高显示布局文件的性能,使用include标签重用layouts
- 提升Android ListView性能的几个技巧
- 提升Android ListView性能的几个技巧
- 提升Android ListView性能的几个技巧
- 提升Android ListView性能的几个技巧
- 提升Android ListView性能的几个技巧
- 提升Android ListView性能的几个技巧
- Java:在Bean中使用PropertyChangeSupport支持PropertyChangeListeners
- C#调用epplus 创建Excel
- cocos2d-X CCsprite点击问题
- In App Purchase Verification using PHP
- 利用CSS3制作WINDOWS7开始菜单
- 提高显示布局文件的性能 4 - 提升ListView的性能
- selenium webdriver定位不到元素的五种原因及解决办法
- hadoop的五个守护进程
- 关闭ASM实例时 ORA-01031: insufficient privileges
- 绑定到异步的ObservableCollection [This type of CollectionView does not support changes to its SourceColl]
- Asp.net 中连接失败跳转到固定页面
- 小红帽的安装
- Linux中IIC总线驱动分析
- 为程序员量身定制的12个目标