android getView()参数含义

ListView中每个子项被滚动到屏幕里的时候会调用getView()方法


看了一下例子不是很明白怎么用,网上查了一下这个方法的用法

public View getView (int position, View convertView, ViewGroup parent)

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.

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.

parent

The parent that this view will eventually be attached to

Returns

A View corresponding to the data at the specified position.


convertView参数用于将之前加载好的布局进行缓存


converView是离开屏幕的布局,例如,列表是第一次显示,这种情况converView为空,当你向下滑动屏幕,第0行会离开屏幕(将不可见),这时ListView可能会把之前的布局进行缓存用于以后使用(这是有意义的,ListView一般有同样的布局,只是数据不同),缓存布局的原因是getView方法会花很多时间(每次用户上下滑动,有新的行出现在屏幕时)。如果每次要重新创建会消耗很多时间,所以要避免这种情况。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值