BaseAdapter中getView()方法详解

上一篇文章中关于优化ListView(使用BaseAdapter)的例子中,getView()这个方法解释的不是很清楚。这次单独写一篇详细解释一下getView方法。


官方API中关于getView的解释

public abstract View getView (int position, View convertView, ViewGroup parent)    Added in API level 1
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use  inflate(int, android.view.ViewGroup, boolean)  to specify a root view and to prevent attachment to the root.

Parameters
position The position of the item within the adapter's data set of the item whose view we want.

convertView The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (seegetViewTypeCount() and getItemViewType(int)).

parent The parent that this view will eventually be attached to

Returns
  • A View corresponding to the data at the specified position.

如果不对BaseAdapter进行优化,当List中的Item很多时会造成大量的资源浪费。
         public View getView(final int position, View convertView, ViewGroup parent) {  
             ViewHolder holder;  

            if (convertView == null) {  
                    convertView = mInflater.inflate(R.layout.item, null);  
                    holder = new ViewHolder();  
                    holder.title = (TextView) convertView.findViewById(R.id.ItemTitle);  
                    holder.text = (TextView) convertView.findViewById(R.id.ItemText);  
                    holder.bt = (Button) convertView.findViewById(R.id.ItemButton);  
                    convertView.setTag(holder);//绑定ViewHolder对象  
                   }  
    <span style="white-space:pre">	</span>    else{  
                    holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象  
                  }  
            //设置TextView显示的内容,即我们存放在动态数组中的数据, <span style="font-family: Roboto, sans-serif;">getDate()为获得数据的函数</span>
            holder.title.setText(getDate().get(position).get("ItemTitle").toString());  
            holder.text.setText(getDate().get(position).get("ItemText").toString());  

            return convertView;  
        }  

上面是一个对getView优化的例子,主要解释以下两点:

1. convertView是拿来复用的

当启动 Activity 呈现第一屏 ListView 的时候, convertView 为零。当用户向下滚动 ListView 时,上面的条目变为不可见,下面出现新的条目。这时候 convertView 不再为空,而是创建了一系列的 convertView 的值。当又往下滚一屏的时候,发现第 11 行的容器用来容纳第 22 行,第 12 行的容器用来容纳第 23 行。也就是说 convertView 相当于一个缓存,开始为 0 ,当有条目变为不可见,它缓存了它的数据,后面再出来的条目只需要更新数据就可以了,这样大大节省了系统资料的开销。
我们所要做的就是将需要显示的item填充到这些回收的view中去。

2. setTag(Object), getTag(Object)

View中的setTag和getTag是为了给View添加一个额外的数据,用到的时候可以取出来。 如果convertview 是第一次展示,new一个ViewHolder与之绑定,如果convertview 之前已经创建过了,就可以直接取出holder对象并加上新的数据。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值