您好,登錄后才能下訂單哦!
Android中列表的復(fù)用機(jī)制提高了APP的運(yùn)行效率,但隨之而來(lái)的復(fù)用的問(wèn)題總是讓程序員們頭痛,一個(gè)
bug找頭天也找不到。我就把自己解決這方面的經(jīng)驗(yàn)貢獻(xiàn)出來(lái)供大家參考:
問(wèn)題1:什么是復(fù)用
復(fù)用其實(shí)指的是復(fù)用View,而綁定View的數(shù)據(jù)是變化的。
問(wèn)題2:復(fù)用的原理探究
為了徹底弄清楚復(fù)用的原理,和特地寫(xiě)了段小程序。
Adapter代碼:
class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.i(TAG, "aaaaaaaaaa---------- getView: position = " + position + ",convertView = " + convertView); ViewHolder holder; if(convertView == null){ convertView = LayoutInflater.from(SimpleCheckBoxListActivity.this).inflate(R.layout.adapter_simple_checkbox_item,null,false); holder = new ViewHolder(); holder.tv = (TextView) convertView.findViewById(R.id.tv); holder.cb = (CheckBox) convertView.findViewById(R.id.cb); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tv.setText("index = " + position); Log.i(TAG, "bbbbbbbbbb---------- getView: position = " + position + ",convertView = " + convertView.toString()); //將convertView緩存起來(lái),方便后面的分析。 itemViews.put(position,convertView); //分析當(dāng)前position是否復(fù)用了之前哪個(gè)位置的view int reusePosition = analyseReusedWhichPosition(position); if(reusePosition != -1){ Log.i(TAG, "getView: 位置 " + position + "復(fù)用了位置" + reusePosition + "的view"); } return convertView; } class ViewHolder{ TextView tv; CheckBox cb; } //分析當(dāng)前position是否復(fù)用了之前哪個(gè)位置的view private int analyseReusedWhichPosition(int currentPosition){ View currentPositionView = itemViews.get(currentPosition); for (int i = 0; i < currentPosition; i++) { View beforePositionView = itemViews.get(i); if(beforePositionView == null){ continue; } if(beforePositionView == currentPositionView){ return i; } } return -1; } }
日志分析:
1)程序初次運(yùn)行
打印的日志:
aaaaaaaaaa---------- getView: position = 0,convertView = null bbbbbbbbbb---------- getView: position = 0,convertView = android.widget.LinearLayout{42eceab0 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 1,convertView = null bbbbbbbbbb---------- getView: position = 1,convertView = android.widget.LinearLayout{42ee4650 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 2,convertView = null bbbbbbbbbb---------- getView: position = 2,convertView = android.widget.LinearLayout{42ee6140 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 3,convertView = null bbbbbbbbbb---------- getView: position = 3,convertView = android.widget.LinearLayout{42ee7c10 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 4,convertView = null bbbbbbbbbb---------- getView: position = 4,convertView = android.widget.LinearLayout{42ee96e0 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 5,convertView = null bbbbbbbbbb---------- getView: position = 5,convertView = android.widget.LinearLayout{42eeb1e8 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 6,convertView = null bbbbbbbbbb---------- getView: position = 6,convertView = android.widget.LinearLayout{42eeccb8 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 7,convertView = null bbbbbbbbbb---------- getView: position = 7,convertView = android.widget.LinearLayout{42eee788 V.E..... ......I. 0,0-0,0}
2)接著向下滑動(dòng),索引0沒(méi)有完全消失,索引8就出現(xiàn)了,這時(shí)還沒(méi)有復(fù)用。
打印的日志:
aaaaaaaaaa---------- getView: position = 8,convertView = null bbbbbbbbbb---------- getView: position = 8,convertView = android.widget.LinearLayout{42ef5150 V.E..... ......I. 0,0-0,0}
3)位置9出現(xiàn),索引0已經(jīng)完全消失(復(fù)用開(kāi)始出現(xiàn))
打印的日志:
aaaaaaaaaa---------- getView: position = 9,convertView = android.widget.LinearLayout{42eceab0 V.E..... ........ 0,-215-1080,1} bbbbbbbbbb---------- getView: position = 9,convertView = android.widget.LinearLayout{42eceab0 V.E..... .......D 0,-215-1080,1} getView: 位置 9復(fù)用了位置0的view 可以發(fā)現(xiàn)索引9處的hashCode與索引0處的hashCode都是42eceab0
4)緊接著向下滾動(dòng)到最后(注意是慢慢地滾動(dòng))
打印的日志:
aaaaaaaaaa---------- getView: position = 10,convertView = android.widget.LinearLayout{42ee4650 V.E..... ........ 0,-213-1080,3} bbbbbbbbbb---------- getView: position = 10,convertView = android.widget.LinearLayout{42ee4650 V.E..... .......D 0,-213-1080,3} getView: 位置 10復(fù)用了位置1的view aaaaaaaaaa---------- getView: position = 11,convertView = android.widget.LinearLayout{42ee6140 V.E..... ........ 0,-205-1080,11} bbbbbbbbbb---------- getView: position = 11,convertView = android.widget.LinearLayout{42ee6140 V.E..... .......D 0,-205-1080,11} getView: 位置 11復(fù)用了位置2的view aaaaaaaaaa---------- getView: position = 12,convertView = android.widget.LinearLayout{42ee7c10 V.E..... ........ 0,-202-1080,14} bbbbbbbbbb---------- getView: position = 12,convertView = android.widget.LinearLayout{42ee7c10 V.E..... .......D 0,-202-1080,14} getView: 位置 12復(fù)用了位置3的view aaaaaaaaaa---------- getView: position = 13,convertView = android.widget.LinearLayout{42ee96e0 V.E..... ........ 0,-201-1080,15} bbbbbbbbbb---------- getView: position = 13,convertView = android.widget.LinearLayout{42ee96e0 V.E..... .......D 0,-201-1080,15} getView: 位置 13復(fù)用了位置4的view aaaaaaaaaa---------- getView: position = 14,convertView = android.widget.LinearLayout{42eeb1e8 V.E..... ........ 0,-188-1080,28} bbbbbbbbbb---------- getView: position = 14,convertView = android.widget.LinearLayout{42eeb1e8 V.E..... .......D 0,-188-1080,28} getView: 位置 14復(fù)用了位置5的view aaaaaaaaaa---------- getView: position = 15,convertView = android.widget.LinearLayout{42eeccb8 V.E..... ........ 0,-213-1080,3} bbbbbbbbbb---------- getView: position = 15,convertView = android.widget.LinearLayout{42eeccb8 V.E..... .......D 0,-213-1080,3} getView: 位置 15復(fù)用了位置6的view aaaaaaaaaa---------- getView: position = 16,convertView = android.widget.LinearLayout{42eee788 V.E..... ........ 0,-179-1080,37} bbbbbbbbbb---------- getView: position = 16,convertView = android.widget.LinearLayout{42eee788 V.E..... .......D 0,-179-1080,37} getView: 位置 16復(fù)用了位置7的view aaaaaaaaaa---------- getView: position = 17,convertView = android.widget.LinearLayout{42ef5150 V.E..... ........ 0,-181-1080,35} bbbbbbbbbb---------- getView: position = 17,convertView = android.widget.LinearLayout{42ef5150 V.E..... .......D 0,-181-1080,35} getView: 位置 17復(fù)用了位置8的view aaaaaaaaaa---------- getView: position = 18,convertView = android.widget.LinearLayout{42eceab0 V.E..... ........ 0,-195-1080,21} bbbbbbbbbb---------- getView: position = 18,convertView = android.widget.LinearLayout{42eceab0 V.E..... .......D 0,-195-1080,21} getView: 位置 18復(fù)用了位置0的view aaaaaaaaaa---------- getView: position = 19,convertView = android.widget.LinearLayout{42ee4650 V.E..... ........ 0,-210-1080,6} bbbbbbbbbb---------- getView: position = 19,convertView = android.widget.LinearLayout{42ee4650 V.E..... .......D 0,-210-1080,6} getView: 位置 19復(fù)用了位置1的view
可以看到向下慢慢滑動(dòng)的時(shí)候,復(fù)用是很有規(guī)律的。
但是如果快速的向下滑動(dòng)的時(shí)候,又發(fā)現(xiàn)不了什么規(guī)律:復(fù)用并非是連續(xù)的
aaaaaaaaaa---------- getView: position = 8,convertView = android.widget.LinearLayout{42f9a780 V.E..... ........ 0,-85-1080,131} bbbbbbbbbb---------- getView: position = 8,convertView = android.widget.LinearLayout{42f9a780 V.E..... .......D 0,-85-1080,131} getView: 位置 8復(fù)用了位置0的view aaaaaaaaaa---------- getView: position = 9,convertView = android.widget.LinearLayout{42f9f818 V.E..... ........ 0,384-1080,600} bbbbbbbbbb---------- getView: position = 9,convertView = android.widget.LinearLayout{42f9f818 V.E..... .......D 0,384-1080,600} getView: 位置 9復(fù)用了位置3的view aaaaaaaaaa---------- getView: position = 10,convertView = android.widget.LinearLayout{42f9dd48 V.E..... ........ 0,138-1080,354} bbbbbbbbbb---------- getView: position = 10,convertView = android.widget.LinearLayout{42f9dd48 V.E..... .......D 0,138-1080,354} getView: 位置 10復(fù)用了位置2的view aaaaaaaaaa---------- getView: position = 11,convertView = android.widget.LinearLayout{42f9c278 V.E..... ........ 0,-108-1080,108} bbbbbbbbbb---------- getView: position = 11,convertView = android.widget.LinearLayout{42f9c278 V.E..... .......D 0,-108-1080,108} getView: 位置 11復(fù)用了位置1的view aaaaaaaaaa---------- getView: position = 12,convertView = null bbbbbbbbbb---------- getView: position = 12,convertView = android.widget.LinearLayout{42fad3a0 V.E..... ......I. 0,0-0,0} aaaaaaaaaa---------- getView: position = 13,convertView = android.widget.LinearLayout{42fa2df0 V.E..... ........ 0,60-1080,276} bbbbbbbbbb---------- getView: position = 13,convertView = android.widget.LinearLayout{42fa2df0 V.E..... .......D 0,60-1080,276} getView: 位置 13復(fù)用了位置5的view aaaaaaaaaa---------- getView: position = 14,convertView = android.widget.LinearLayout{42fa12e8 V.E..... ........ 0,-186-1080,30} bbbbbbbbbb---------- getView: position = 14,convertView = android.widget.LinearLayout{42fa12e8 V.E..... .......D 0,-186-1080,30} getView: 位置 14復(fù)用了位置4的view aaaaaaaaaa---------- getView: position = 15,convertView = android.widget.LinearLayout{42fa48c0 V.E..... ........ 0,-150-1080,66} bbbbbbbbbb---------- getView: position = 15,convertView = android.widget.LinearLayout{42fa48c0 V.E..... .......D 0,-150-1080,66} getView: 位置 15復(fù)用了位置6的view aaaaaaaaaa---------- getView: position = 16,convertView = android.widget.LinearLayout{42f9a780 V.E..... ........ 0,78-1080,294} bbbbbbbbbb---------- getView: position = 16,convertView = android.widget.LinearLayout{42f9a780 V.E..... .......D 0,78-1080,294} getView: 位置 16復(fù)用了位置0的view aaaaaaaaaa---------- getView: position = 17,convertView = android.widget.LinearLayout{42f9f818 V.E..... ........ 0,13-1080,229} bbbbbbbbbb---------- getView: position = 17,convertView = android.widget.LinearLayout{42f9f818 V.E..... .......D 0,13-1080,229} getView: 位置 17復(fù)用了位置3的view aaaaaaaaaa---------- getView: position = 18,convertView = android.widget.LinearLayout{42f9dd48 V.E..... ........ 0,-28-1080,188} bbbbbbbbbb---------- getView: position = 18,convertView = android.widget.LinearLayout{42f9dd48 V.E..... .......D 0,-28-1080,188} getView: 位置 18復(fù)用了位置2的view aaaaaaaaaa---------- getView: position = 19,convertView = android.widget.LinearLayout{42fa6390 V.E..... ........ 0,-168-1080,48} bbbbbbbbbb---------- getView: position = 19,convertView = android.widget.LinearLayout{42fa6390 V.E..... .......D 0,-168-1080,48} getView: 位置 19復(fù)用了位置7的view
5)最后,向上滾動(dòng)到索引為0的位置
aaaaaaaaaa---------- getView: position = 11,convertView = android.widget.LinearLayout{4304de70 V.E..... ........ 0,-212-1080,4} bbbbbbbbbb---------- getView: position = 11,convertView = android.widget.LinearLayout{4304de70 V.E..... .......D 0,-212-1080,4} getView: 位置 11復(fù)用了位置8的view aaaaaaaaaa---------- getView: position = 10,convertView = android.widget.LinearLayout{4303ee70 V.E..... ........ 0,1829-1080,2045} bbbbbbbbbb---------- getView: position = 10,convertView = android.widget.LinearLayout{4303ee70 V.E..... .......D 0,1829-1080,2045} getView: 位置 10復(fù)用了位置1的view aaaaaaaaaa---------- getView: position = 9,convertView = android.widget.LinearLayout{43040940 V.E..... ........ 0,1829-1080,2045} bbbbbbbbbb---------- getView: position = 9,convertView = android.widget.LinearLayout{43040940 V.E..... .......D 0,1829-1080,2045} getView: 位置 9復(fù)用了位置2的view aaaaaaaaaa---------- getView: position = 8,convertView = android.widget.LinearLayout{4303d378 V.E..... ........ 0,1830-1080,2046} bbbbbbbbbb---------- getView: position = 8,convertView = android.widget.LinearLayout{4303d378 V.E..... .......D 0,1830-1080,2046} getView: 位置 8復(fù)用了位置0的view aaaaaaaaaa---------- getView: position = 7,convertView = android.widget.LinearLayout{430474b8 V.E..... ........ 0,1825-1080,2041} bbbbbbbbbb---------- getView: position = 7,convertView = android.widget.LinearLayout{430474b8 V.E..... .......D 0,1825-1080,2041} getView: 位置 7復(fù)用了位置6的view aaaaaaaaaa---------- getView: position = 6,convertView = android.widget.LinearLayout{43048f88 V.E..... ........ 0,1824-1080,2040} bbbbbbbbbb---------- getView: position = 6,convertView = android.widget.LinearLayout{43048f88 V.E..... .......D 0,1824-1080,2040} aaaaaaaaaa---------- getView: position = 5,convertView = android.widget.LinearLayout{43043ee0 V.E..... ........ 0,1822-1080,2038} bbbbbbbbbb---------- getView: position = 5,convertView = android.widget.LinearLayout{43043ee0 V.E..... .......D 0,1822-1080,2038} getView: 位置 5復(fù)用了位置4的view aaaaaaaaaa---------- getView: position = 4,convertView = android.widget.LinearLayout{430459e8 V.E..... ........ 0,1823-1080,2039} bbbbbbbbbb---------- getView: position = 4,convertView = android.widget.LinearLayout{430459e8 V.E..... .......D 0,1823-1080,2039} aaaaaaaaaa---------- getView: position = 3,convertView = android.widget.LinearLayout{43042410 V.E..... ........ 0,1829-1080,2045} bbbbbbbbbb---------- getView: position = 3,convertView = android.widget.LinearLayout{43042410 V.E..... .......D 0,1829-1080,2045} aaaaaaaaaa---------- getView: position = 2,convertView = android.widget.LinearLayout{4304de70 V.E..... ........ 0,1826-1080,2042} bbbbbbbbbb---------- getView: position = 2,convertView = android.widget.LinearLayout{4304de70 V.E..... .......D 0,1826-1080,2042} aaaaaaaaaa---------- getView: position = 1,convertView = android.widget.LinearLayout{4303ee70 V.E..... ........ 0,1828-1080,2044} bbbbbbbbbb---------- getView: position = 1,convertView = android.widget.LinearLayout{4303ee70 V.E..... .......D 0,1828-1080,2044} aaaaaaaaaa---------- getView: position = 0,convertView = android.widget.LinearLayout{43040940 V.E..... ........ 0,1788-1080,2004} bbbbbbbbbb---------- getView: position = 0,convertView = android.widget.LinearLayout{43040940 V.E..... .......D 0,1788-1080,2004}
如上所述,到底誰(shuí)復(fù)用了誰(shuí)是隨機(jī)不定的,這個(gè)我們也沒(méi)有必要去關(guān)心。我們只要知道position是不變的就行了。
另外,除了打日志??梢赃x中某一個(gè)位置的checkbox,然后上下滑動(dòng),如果某個(gè)checkbox也莫名的選中了,那就說(shuō)明這個(gè)位置的checkbox復(fù)用了之前選中的那個(gè)checkbox。
問(wèn)題3:Adapter的notifyDataSetChanged()方法作了什么事情
notifyDataSetChanged,會(huì)重新走一遍可見(jiàn)的position的getView方法。
問(wèn)題4:復(fù)用出現(xiàn)的場(chǎng)景
1.if-else的坑:在Adapter中,如果綁定View的數(shù)據(jù)的時(shí)候如果有if判斷,往往很多人忘記了加else,這是大多數(shù)復(fù)用問(wèn)題出現(xiàn)的根源之一。在一般情況下else不寫(xiě)沒(méi)有邏輯錯(cuò)誤,但是在ListView復(fù)
用的情況下如果不寫(xiě)錯(cuò)誤就會(huì)帶來(lái)錯(cuò)亂的麻煩。
實(shí)際場(chǎng)景:
比如每個(gè)item可能有或沒(méi)有圖片picarrList,之前我只加了if判斷,如果有圖片就顯示。但后來(lái)上下一滑動(dòng)之后發(fā)現(xiàn)沒(méi)有圖片的item竟然也顯示了其它了item的圖片,于是追根溯源發(fā)現(xiàn)是這里的問(wèn)題。
2.checkbox等的復(fù)用問(wèn)題:果如下圖,是一個(gè)簡(jiǎn)單的CheckBox列表
第1頁(yè)剛好0-8索引,我將0索引處的checkbox設(shè)置為選中狀態(tài),然后向下滑動(dòng),發(fā)現(xiàn)下一個(gè)出現(xiàn)的checkbox(索引為10,不是9,也不一定就是10,而是索引0完全消失之后第一個(gè)出現(xiàn)的item)竟然也選中了。
百度了一下,可以用Map<Interger,Boolean>來(lái)記錄對(duì)應(yīng)position的checkbox的選中狀態(tài)。而且網(wǎng)上
的這個(gè)Map是事先就是預(yù)訂好大小的了,但實(shí)際中Map的大小是確定的。
細(xì)節(jié)1):Map<Interger,Boolean>來(lái)記錄對(duì)應(yīng)position的checkbox的選中狀態(tài),怎么初始化?
--1-- 可以先在成員或者構(gòu)造方法里實(shí)例化Map對(duì)象
Map<Integer,Boolean> isTitleCheckBoxSelected = new HashMap();
--2-- 在getView方法里初始化Map對(duì)象,默認(rèn)checkbox都是未選中狀態(tài)
if(!isTitleCheckBoxSelected.containsKey(position)){ Log.i(TAG, "bindData: init checkbox " + position); isTitleCheckBoxSelected.put(position,false); //如果啟動(dòng)了全選,則新出現(xiàn)的view也要選中。 if(isSelectedAllStarted){ isTitleCheckBoxSelected.put(position,true); } }
上面的這段代碼其實(shí)是非常妙的,通過(guò)contains判斷,保證了初始化。如果后面操作了map,
也不會(huì)影響這段代碼對(duì)map的初始化。
map這種數(shù)據(jù)結(jié)構(gòu),由于key是唯一,可以做去重操作。這一點(diǎn)List則不可直接做到。
細(xì)節(jié)2):響應(yīng)checkbox的OnCheckedChangeListener事件,將改變后的狀態(tài)保存到map中。
header_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isCheckedByCode) return; isTitleCheckBoxSelected.put(position, !isTitleCheckBoxSelected.get(position)); } });
在onCheckedChanged方法里將對(duì)應(yīng)position的checkbox的狀態(tài)反轉(zhuǎn)。
細(xì)節(jié)3):將map中的對(duì)應(yīng)position的狀態(tài)值賦值給當(dāng)前的checkbox
但是有個(gè)問(wèn)題,checkbox的setChecked方法,看其源碼,會(huì)走OnCheckedChangeListener的回調(diào)
而此時(shí),setChecked方法我只想設(shè)置View的狀態(tài),并不想走它的回調(diào)方法。下面有2種方法可以解決這個(gè)問(wèn)題
方法1:在setChecked方法的前后用一個(gè)變量夾住,在回調(diào)方法里通過(guò)這個(gè)變量判斷回調(diào)是不是在代碼
里通過(guò)setChecked觸發(fā),如果是setChecked觸發(fā)的,則不執(zhí)行map的取反的操作。
isCheckedByCode = true; header_checkbox.setChecked(isTitleCheckBoxSelected.get(position)); isCheckedByCode = false;
這種方法多申請(qǐng)了個(gè)變量,耦合度比較高。
方法2:在setChecked方法之前將checkbox的監(jiān)聽(tīng)設(shè)置為null,在setChecked方法之后設(shè)置真正的監(jiān)聽(tīng)。
除了checkbox,其它的一些view,也可以通過(guò)以上的方法來(lái)解決復(fù)用的問(wèn)題。解決復(fù)用要遵循一個(gè)原則:MV分離,在view一些事件監(jiān)聽(tīng)里,一般情況下改變記錄狀態(tài)的Map值之后,切記立馬就將值設(shè)置給View,而應(yīng)該通過(guò)notifyDatasetChanged()方法將狀態(tài)更新到view上。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。