我相信我對你的需求的解決方案,
在視圖文件中的代碼粘貼
SparseBooleanArray singleChecked;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position != singleListSelectedPosition) {
singleListSelectedPosition = position;
int totalCount = lvSingleSelect.getCount();
for (int i = 0; i < totalCount; i++) {
if (i == position) {
boolean stat = singleChecked.get(position, false);
singleChecked.put(position, !stat);
} else {
singleChecked.put(i, true);
}
}
adapter.setChecked(singleChecked);
}
}
這是您的適配器類代碼:
public void setChecked(SparseBooleanArray ch) {
singleChecked = ch;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (singleChecked.get(position, false)) {
convertView.setBackgroundColor(getResources()
.getColor(R.color.titlebar_background_color));
} else {
convertView.setBackgroundColor(
getResources().getColor(R.color.emphasis_color));
}
請讓我知道如果你有任何麻煩,總是樂於幫助。
沒有你所寫的listviewTitles.setBackgroundColor(非選定的顏色);在其他部分 –
你想突出顯示一個特定的單元格?如果是這樣的話,你也可以設置你的單元佈局的背景,像這樣linearLayout.setBackgroundResource(COLOR.GREY); –
請將整個代碼放在getView()中---檢查您是否正在回收列表中的視圖,因爲如果您選擇的項目在回收時已被選中!如果多數民衆贊成什麼發生! – shadygoneinsane