我一直试图取消选择我的listView
选项,并尝试了在线列出的所有方法,但它根本不会取消选择:
<ListView
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="169dp"
android:id="@+id/listview"
android:choiceMode="singleChoice"
android:listSelector="#666666">
现在,当我想取消选择时,我有以下代码:
listView.setItemChecked(position, false);
listView.clearChoices();
listView.requestLayout();
listView.setSelected(false);
adapter.notifyDataSetChanged();
但是,listView
中的所选项目会保持灰色(#666666)并且不会取消选中。
如何从我的listView
取消选择所选项?
答案 0 :(得分:0)
这是解决方案
ListView listPerkings = (ListView) findViewById(R.id.perkiings);
listPerkings .setChoiceMode(ListView.CHOICE_MODE_SINGLE);
SparseBooleanArray checked = new SparseBooleanArray();
listPerkings .setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView text1 = (TextView) view.findViewById(R.id.perkLabel);
int len = listPerkings .getCount();
checked = listPerkings .getCheckedItemPositions();
for (int i = 0; i < len; i++) {
if (i == position) {
if (checked.get(i)) {
text1.setTextColor(getResources().getColor(
R.color.perks_selected_item_text_color));
} else {
text1.setTextColor(getResources().getColor(
R.color.perks_default_item_text_color));
}
}
}
}
});