In this case I think you need to manage it from your ArrayAdapter
class.
There you have to remember last clicked item view and position.
Basic idea is onListItemClick
pass the clicked position and itemview to adapter there
we will set it's background with the color you like and changed the last selected to view
background color to default color.
public class SampleAdapter extends ArrayAdapter<Object> {
private int mSelection = 0;
public SampleAdapter(Context context, int resource, int textViewResourceId,
List<Object> objects) {
super(context, resource, textViewResourceId, objects);
}
public void setSelection(int mSelection, View selectedItemView) {
this.mSelection = mSelection;
if (selectedItemView!= null && lastSelectedRow != null
&& selectedItemView!= lastSelectedRow) {
lastSelectedRow
.setBackgroundResource(R.drawable.bg_normal);
selectedItemView
.setBackgroundResource(R.drawable.bg_selected);
}
this.lastSelectedRow = selectedItemView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Usual code logic here....
if (mSelection == position) {
mViewHolder.mRootView
.setBackgroundResource(R.drawable.bg_selected);
lastSelectedRow = mViewHolder.mRootView;
} else {
mViewHolder.mRootView
.setBackgroundResource(R.drawable.bg_normal);
}
return view;
}
private static class ViewHolder {
TextView name;
View mRootView;
}
}
On List Item click you need to pass clicked item and position to adapter.
public void onItemClick(AdapterView<?> arg0, View listItemView,
int position, long id) {
if(myAdapter != null )
{
myAdapter.setSelection(position,listItemView);
}
}
if you want to call this set selection from other point
you can call it like this.
myAdapter.setSelection(position,mListViwe.getChildAt(pos));
mListViwe.setSelectionFromTop(position, 0);