Can't "unselect" listview item
I was able to get the background changed of an individual listview item in a setOnItemClickListener with
view.setBackgroundResource(R.color.green);
I only need one selected at a time so when the other list items are clicked, I tried lv.invalidate()
and lv.getChildAt(0).invalidate()
but neither worked and the second causes null pointer exception. Any开发者_如何转开发 ideas for putting the color back?
You can use clearChoices()
method of android.widget.ListView.
http://developer.android.com/reference/android/widget/AbsListView.html#clearChoices()
I am doing some split screen stuff and xml selectors do not work. To set the color back, I ended up storing the view that was clicked in View currentlySelectedView
and setting the background transparent when another view was clicked.
currentlySelectedView.setBackgroundResource(R.color.transparent);
Set a selector as the background of the views that you include in the Adapter. In the selector you can set the color when the item is pressed, focus, and unpress. Thats the right way to do it.
Have a look to this thread Android ListView Selector Color
There is another "workaround" that I think is worth mentioning. You can create a custom adapter class for your ListView that will select(highlight) or deselect(unhighlight) the item for you.
//you can extend whatever kind of adapter you want
public class AutoSelectingListViewAdapter extends ArrayAdapter<Thingy>{
private LayoutInflater inflater;
public boolean shouldHighlight = false;
public int highlightIndex = -1;
public AutoSelectingListViewAdapter(Context context, int resourceId, List<Thingy> objects) {
super(context, resourceId, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ItemHolder holder = null;
if (convertView == null){
// always recycle!
holder = new ItemHolder();
convertView = inflater.inflate(R.layout.custom_list_item, null);
holder.clue = (TextView) convertView.findViewById(R.id.custom_list_item_text);
convertView.setTag(holder);
}else{
holder = (ItemHolder) convertView.getTag();
}
// fake highlighting!
if (shouldHighlight && highlightIndex == position){
convertView.setBackgroundColor(0xffb5bfcc); // put your highlight color here
}else{
convertView.setBackgroundColor(0x00000000); // transparent
}
Thingy thingy = this.getItem(position);
holder.clue.setText(thingy.textData);
return convertView;
}
class ItemHolder{
TextView text;
// any other views you need here
}
}
with this class, you can then manually highlight an item by doing this:
targetListView.setSelection(4); // give item 4 focus
AutoSelectingListViewAdapter myAdapter = (AutoSelectingListViewAdapter) targetListView.getAdapter();
myAdapter.highlightIndex = 4; // highlight item 4
myAdapter.shouldHighlight = true;
myAdapter.notifyDataSetChanged(); // force listview to redraw itself
you can also unhighlight:
myAdapter.shouldHighlight = false;
myAdapter.notifyDataSetChanged();
The following method has worked for me in OnItemClickListener.onItemClick
and has cleared the selected list row:
adapter.notifyDataSetInvalidated();
精彩评论