3 回答

TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊
getView
第一階段:為循環(huán)再造而制作的物品( convertView
是 null
):這意味著創(chuàng)建布局和所有項共享的公共狀態(tài)。如果您有偵聽器,您可以在這里添加它們,并設(shè)計它們,以便它們能夠在以后的位置更改(當(dāng)它被重用時)作出反應(yīng)。因此,例如,通過將位置設(shè)置為相應(yīng)視圖上的標(biāo)記,監(jiān)聽器可以捕獲該信息,并知道它當(dāng)前操作的項目。不能使用視圖存儲數(shù)據(jù)。因此,當(dāng)偵聽器更改列表項上的狀態(tài)時,應(yīng)將此數(shù)據(jù)持久化(在數(shù)據(jù)數(shù)組中、SQLite數(shù)據(jù)庫中等),并在 第二階段.第二階段:設(shè)置給定位置的項目狀態(tài):
設(shè)置項目的可視狀態(tài)。對于某一項可能單獨更改的所有內(nèi)容(文本、復(fù)選框狀態(tài)、顏色等)都必須在這里設(shè)置。不僅更改了當(dāng)前項的內(nèi)容,而且還可能被另一項更改。通過這種方式,可以確保視圖不用于 無效狀態(tài),因為它以前是從另一個列表項中重用的。
getItemViewType
getViewTypeCount
getItemViewType
getViewTypeCount
void getItemViewType(int position) { return isItemAtPositionSeperator(position) ? 1 : /* normal item */ 0;}void int getViewTypeCount() { return 2; // normal item and separator}void View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); // phase 1: see my explanation if (convertView == null) { if (type == 0) { // setup your common item view - inflate it and set to convertView } else { // setup separator view - inflate it and set to convertView } } // phase 2: see my explanation if (type == 0) { // set the state of the common item view based on the position // rely on the fact that convertView contains the view hierarchy // you created in convertView == null && type == 0 } else { // set state of the separator based on the position // rely on the fact that convertView contains the view hierarchy // you created in convertView == null && type != 0 (else part) } return convertView;}
問題的實際答案.。
viewHolder.checkbox.setOnCheckedChangeListener
final position
viewHolder.checkbox.setTag(position)
return
(Integer) buttonView.getTag()
position+1
viewHolder.checkbox.setChecked(persistedState)
return
.
- 3 回答
- 0 關(guān)注
- 445 瀏覽
添加回答
舉報