ListView优化adapter getview的两种方式ViewHolder vs HolderView

来源:互联网 发布:windows凭据总丢失 编辑:程序博客网 时间:2024/05/18 01:14

一、ViewHolder方式

如果你还没听说过ViewHolder,那么你该去好好看看官方文档了,而不是埋头写代码。

一个ListView的item布局中需要赋值的子元素太多为了避免重复的调用FindViewById方法,我们一般考虑使用ViewHolder方式来实现BaseAdapter。

如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //在外面先定义,ViewHolder静态类  
  2. static class ViewHolder  
  3. {  
  4.     public ImageView img;  
  5.     public TextView title;  
  6.     public TextView info;  
  7. }  
  8. //然后重写getView  
  9. @Override  
  10. public View getView(int position, View convertView, ViewGroup parent) {  
  11.     ViewHolder holder;  
  12.     if(convertView == null)  
  13.     {  
  14.         holder = new ViewHolder();  
  15.         convertView = mInflater.inflate(R.layout.list_item, null);  
  16.         holder.img = (ImageView)item.findViewById(R.id.img)  
  17.         holder.title = (TextView)item.findViewById(R.id.title);  
  18.         holder.info = (TextView)item.findViewById(R.id.info);  
  19.         convertView.setTag(holder);  
  20.     }else  
  21.     {  
  22.         holder = (ViewHolder)convertView.getTag();  
  23.     }  
  24.         holder.img.setImageResource(R.drawable.ic_launcher);  
  25.         holder.title.setText("Hello");  
  26.         holder.info.setText("World");  
  27.     }  
  28.                                                                                                                                                                                                                                                                                                                                                   
  29.     return convertView;  
  30. }  

ViewHolderconvertView第一次inflate的时候绑定了相关的子元素,并被convertView保存下来(setTag方法),当相同的convertView再次需要显示的时候直接调用convertView的getTag取出ViewHolder,并对ViewHolder中的元素赋值。使用ViewHolder模式避免了没有必要的调用findViewById():因为太多的findViewById也会影响性能,这点不容易考虑到。


二、HolderView方式

在HolderView方式中,目的同样是为了避免反复的调用findViewById,但是我们将这个findViewByIdde 任务交给了一个HolderView对象

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. public View getView(int i, View convertView, ViewGroup viewGroup) {  
  3.     HolderView holderView;  
  4.     // Important to not just null check, but rather to a instanceof  
  5.     // since we might get any subclass of view here.  
  6.     if (convertView instanceof HolderView) {  
  7.         holderView = (HolderView) convertView;  
  8.     } else {  
  9.         holderView = new HolderView(mContext);  
  10.     }  
  11.     holderView.bind("标题", R.drawable.ic_launcher, "sajsa");  
  12.     return holderView;  
  13. }  
  14. public class HolderView extends GridLayout {  
  15.     private ImageView img;  
  16.     private TextView title;  
  17.     private TextView info;  
  18.     public HolderView(Context context, AttributeSet attrs) {  
  19.         super(context, attrs);  
  20.         View v = LayoutInflater.from(context).inflate(R.layout.list_item, this);  
  21.         title = (TextView) v.findViewById(R.id.title);  
  22.         img = (ImageView)item.findViewById(R.id.img)  
  23.         info = (TextView)item.findViewById(R.id.info);  
  24.     }  
  25.     public void bind(String stringtitle,int imgrsc, String stringinfo) {  
  26.         title.setText(stringtitle);  
  27.         img.setImageResource(imgrsc);  
  28.         info.setText(stringinfo);  
  29.     }  
  30. }  

HolderView自己维护一个子元素的集合,同时对外提供绑定数据的公共方法bind。HolderView方式的思想是:对于ListView来讲,每一个Item本身是同一个类只是数据不同。但是需要注意的是使用HolderView方式在Adapter中getView返回的是HolderView对象。

总结:ViewHolder方式使用简单,HolderView方式更符合面向对象规范。

0 0
原创粉丝点击