我相信我对你的需求的解决方案,
在视图文件中的代码粘贴
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