GridView重复调用getView有关问题

使用GridView控件,却发现getView被重复调用,次数多达上百次,拖垮了系统,影响用户体验!

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

		Log.v(Tag, "<getView> position = " + position);
		
		...
		return convertView;
	}

log信息:
10-12 10:43:09.880: V/GridViewAdapter(30785): <getView> position = 0
10-12 10:43:09.890: V/GridViewAdapter(30785): <getView> position = 0
10-12 10:43:09.890: V/GridViewAdapter(30785): <getView> position = 0
10-12 10:43:09.900: V/GridViewAdapter(30785): <getView> position = 0
10-12 10:43:09.910: V/GridViewAdapter(30785): <getView> position = 0


其中只有以下五条信息是正常的,其它都是多余。

10-12 10:43:10.160: V/GridViewAdapter(30785): <getView> position = 0
10-12 10:43:10.160: V/GridViewAdapter(30785): <getView> position = 1
10-12 10:43:10.170: V/GridViewAdapter(30785): <getView> position = 2
10-12 10:43:10.200: V/GridViewAdapter(30785): <getView> position = 3
10-12 10:43:10.220: V/GridViewAdapter(30785): <getView> position = 4

参考了网上很多的修改的方法,比如布局中高度属性wrap_content设为固定值或者fill_parent等,但都未起作用。也许遇到问题太特殊,只能自己想办法了!      

虽然没有办法阻止系统重复调用getView,但是我们有办法让多余的getView什么都不做。如此这般就可以减轻系统负担,增加用户体验。  

增加一个变量mCount来记录position = 0的次数,依据mCount的值来决定执行流程。

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

		Log.v(Tag, "<getView> position = " + position + " mCount = " + mCount);
		

		if (position == 0)
		{
			mCount++;
		}
		else
		{
			mCount = 0;
		}
		
		if (mCount > 1)
		{
			Log.v(Tag, "<getView> drop !!!");
			return convertView;
		}

		...
		return convertView;
	}


log信息

10-12 12:57:32.436: V/GridViewAdapter(32222): <getView> position = 0 mCount = 0
10-12 12:57:32.436: V/GridViewAdapter(32222): <getView> position = 0 mCount = 1
10-12 12:57:32.436: V/GridViewAdapter(32222): <getView> drop !!!
10-12 12:57:32.446: V/GridViewAdapter(32222): <getView> position = 0 mCount = 2
10-12 12:57:32.456: V/GridViewAdapter(32222): <getView> drop !!!
10-12 12:57:32.456: V/GridViewAdapter(32222): <getView> position = 0 mCount = 3
10-12 12:57:32.456: V/GridViewAdapter(32222): <getView> drop !!!
10-12 12:57:32.466: V/GridViewAdapter(32222): <getView> position = 0 mCount = 4
10-12 12:57:32.466: V/GridViewAdapter(32222): <getView> drop !!!

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值