原因就是:

1. 使用convertView可以避免重复地调用inflate

2. 使用ViewHolder可以避免重复地调用findViewById


下面的getView代码来自于

[plain] view plain copy
  1. development/samples/ApiDemos/src/com/example/android/apis/view/List14.java



[java] view plain copy
  1. /**
  2. *Makeaviewtoholdeachrow.
  3. *
  4. *@seeandroid.widget.ListAdapter#getView(int,android.view.View,
  5. *android.view.ViewGroup)
  6. */
  7. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  8. //AViewHolderkeepsreferencestochildrenviewsto<spanstyle="color:#ff0000;">avoidunneccessarycalls
  9. //tofindViewById()</span>oneachrow.
  10. ViewHolderholder;
  11. //WhenconvertViewisnotnull,wecanreuseitdirectly,<spanstyle="color:#ff0000;">thereisnoneed
  12. //toreinflateit.</span>WeonlyinflateanewViewwhentheconvertViewsupplied
  13. //byListViewisnull.
  14. if(convertView==null){
  15. convertView=mInflater.inflate(R.layout.list_item_icon_text,null);
  16. //CreatesaViewHolderandstorereferencestothetwochildrenviews
  17. //wewanttobinddatato.
  18. holder=newViewHolder();
  19. holder.text=(TextView)convertView.findViewById(R.id.text);
  20. holder.icon=(ImageView)convertView.findViewById(R.id.icon);
  21. convertView.setTag(holder);
  22. }else{
  23. //GettheViewHolderbacktogetfastaccesstotheTextView
  24. //andtheImageView.
  25. holder=(ViewHolder)convertView.getTag();
  26. }
  27. //Bindthedataefficientlywiththeholder.
  28. holder.text.setText(DATA[position]);
  29. holder.icon.setImageBitmap((position&1)==1?mIcon1:mIcon2);
  30. returnconvertView;
  31. }

更多相关文章

  1. Android(安卓)Activity 二
  2. startActivity调用流程及生命周期
  3. [原][Android]All WebView methods must be called on the same
  4. ContentProvider原理分析二 MediaProvider publish .
  5. Android系统Intent中的Uri使用
  6. 【Android开发经验】FaceBook推出的Android图片加载库-Fresco
  7. viewpage setOnPageChangeListener 监听的详解
  8. IntentService解析
  9. android 调用视图报错The specified child already has a parent

随机推荐

  1. android Menory 小结
  2. Android资源加载机制
  3. [Android(安卓)SQLite]数据存储与访问 -
  4. ActivityThread-activity启动分析
  5. android中检测网络是否断开
  6. Android(安卓)编辑联系人,增、删、改代码
  7. cocos2d-js hello world
  8. Android(安卓)创建activity流程-基于api2
  9. Android(安卓)Studio重写方法时参数显示
  10. android之Animation的基本使用