Google I/o 2009 android 笔记 Adapter 与开发高效的UI
来源:互联网 发布:莱克电气知乎 编辑:程序博客网 时间:2024/05/16 09:35
在Android 中创建一个View是比较昂贵的, 尤其从xml中创建,需要解析,反射etc.
当一个ListView中有很多View的时候创建这些View将消耗很多的资源,android 使用了利用回收再利用机制来确保程序的流畅。
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
当这个ListView 向上滚动时需要创建一个Item9 同时,Item1 不在显示区域将看不到,所以android 将会把item1 的 引用传递给 Adapter.getView() 中的convertView ;我们也不用担心convertView 是不是正确的类型,这个由系统保证,所以我们要作的就是把convertView 转换成我们自己的View 再给它赋值,in this case (TextView) convertView.setText(“Item9”);
Don’t
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 但还可以更好
static class ViewHolder {//用来存放View 这样就不用再使用convertView.findViewById()
//了,因为访问属性要比访问方法便宜得多
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();
}
//we store the data ,so that we don’t have to use findViewById();
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
以下是前三段代码的性能比较
自定义View 需要刷新的时候不要刷新整个View
而应该刷新有意义的Rect部分
Dirty regions
-invalidate(Rect)
-invalidate(left ,top, right, bottom)
如果一个View 长时间在处于Visibility GONE,那么使用ViewStub 组件
这样可以减少Activity 启动时的负担,关于ViewStub 用法以后再更新。
Use ViewStub instead of using view.setVisibility(GONE);
- Google I/o 2009 android 笔记 Adapter 与开发高效的UI
- Android开发——09Google I/O之让Android UI性能更高效(1)
- Android开发——09Google I/O之让Android UI性能更高效(1)
- Android开发——09Google I/O之让Android UI性能更高效(1)
- Google I/O之让Android UI性能更高效
- Google I/O 2009 Make your Android UI Fast and Efficient
- Google I/O 2014 大会总结 Android开发新方向
- Google I O 2017 之Android开发特性
- Google I/O 2016 笔记:APK 瘦身的正确姿势
- 从Google I/O 2012看 Android 4.1的变化
- Google公布I\/O 2017 for Android的源代码
- Google I/O 2014? No,Android I/O 2014
- Google I/O 2011, Android Accelerated Rendering
- Google I/O 2013隆重推出Android Studio
- Google I/O 2014 - Keynote for Android
- Google I/O 2014之Android面面观
- Google I/O 2014之Android面面观
- Google I/O 2014之Android面面观
- ORACLE根据汉字返回拼音的包--强大
- 如何制作PPT模版
- 从dll文件导出对应的lib文件
- 关于打印(c#套打...)
- C语言```预处理
- Google I/o 2009 android 笔记 Adapter 与开发高效的UI
- main(int argc char**argv)具体用法
- 上海银行房贷增速减缓 二套房贷平均首付64%
- python 文件与目录操作
- 农行500亿次级债获批
- intent activity action
- C语言课程设计 贪吃蛇代码
- 将linux文件夹映射为网络盘,并使用tfs,无效!
- 显微镜知识,显微镜的光学故障