閱讀894 返回首頁    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 素數篩法