阅读894 返回首页    go 阿里云 go 技术社区[云栖]


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 !!!

最后更新:2017-04-04 07:03:27

  上一篇:go POJ2447 分解因数+扩展欧几里得+高次幂取模
  下一篇:go POJ3306 素数筛法