I need to highlight a row in a ListView
that was selected (to show the user what he chose), so, it\'s not the one that is going to be chosen, it\'s the one he c
I solve this problem in the following way: 1. set a lastClickId, when click the item in listView, update the lastClickId to position value, then update the view's background. After this, when we click one item, this item will be highlighted, but when we scroll the listView(make the item which we selected out of the screen) and scroll back, the highlight is gone, because the method getView() rerun in your adapter, so, we have to do the next thing. 2. in your adapter, change the background in the method getView(), here is the code:
private static int lastClickId = -1;
private OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if ((lastClickId != -1) && (lastClickId != position)) {
parent.getChildAt(lastClickId).setBackgroundResource(
R.color.grey);
view.setBackgroundResource(R.color.blue);
}
if (lastClickId == -1) {
view.setBackgroundResource(R.color.blue);
}
lastClickId = position;
}
};
public static int getCurrentSelectedItemId() {
return lastClickId;
}
Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = mInflater.inflate(R.layout.tweet_list_layout, null);
// set selected item's background to blue
if (position == MainUserFeedFragment.getCurrentSelectedItemId()) {
view.setBackgroundResource(R.color.blue);
}
}
Since by default ListViews
are set to a selection mode of NONE
, in touch mode the setSelection
method won't have visual effect.
For keeping the previous selection / visually display an explicit selection, first you must set your listview's choice mode appropriately:
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
It's useful to read the API Docs of these methods:
void android.widget.AdapterView.setSelection(int position)
Sets the currently selected item. To support accessibility subclasses that override this method must invoke the overriden super method first.
Parameters:
position
Index (starting at 0) of the data item to be selected.
void android.widget.ListView.setChoiceMode(int choiceMode)
Defines the choice behavior for the List. By default, Lists do not have any choice behavior (
CHOICE_MODE_NONE
). By setting the choiceMode toCHOICE_MODE_SINGLE
, the List allows up to one item to be in a chosen state. By setting the choiceMode toCHOICE_MODE_MULTIPLE
, the list allows any number of items to be chosen.Parameters:
choiceMode
One ofCHOICE_MODE_NONE
,CHOICE_MODE_SINGLE
, orCHOICE_MODE_MULTIPLE
In case this is not enough (say you'd like to always show the last selection differently beside the current selection), you should store your last selected item (a data which populates the ListAdapter
) as lastSelectedItem
, and in your adapter's getView
method assign a different background resource to the renderer if it equals this lastSelectedItem
.
If your last selection wouldn't refresh on selection change, you should explicitly call the notifyDataSetChanged
method on your adapter instance.
Update
Since your activity containing the ListView
is a child of an activity which waits for this one's result (based on the setResult(Activity.RESULT_OK,pongIntent);
part), the initial idea is correct, the last position should be passed through the intent when starting the activity:
selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);
The ListView.CHOICE_MODE_SINGLE
solution would work if you remain in the same activity, but you are finishing it on every itemClick (selection change), that's why the extra data should be passed to the starting Intent
.
You can also set the previously selected item's background from your adapter -as mentioned above-, overriding its getView
method:
lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final View renderer = super.getView(position, convertView, parent);
if (position == selectedListItem)
{
//TODO: set the proper selection color here:
renderer.setBackgroundResource(android.R.color.darker_gray);
}
return renderer;
}
});