So I have an activity with 2 ListView
widgets, when you select a value in the first one, the second is populated with values related to the selection in the first ListView
. This mechanic works without a problem, but now I want the user choices to stay highlighted. I've read a good ammount of question related to this topic and it seems there is a myriad of ways one can accomplish this but after trying about 4-5 of em' I still can't get it to work.
I've got it working on the second ListView
using the android:listSelector="#CCCCCC"
XML Attribute, but this seems to be wiped clean once a OnItemClickListener
is introduced into the mix (like the one I use on my first ListView
).
So far here's what I've got:
Custom OnItemClickListener
I found browsing various answer regarding this topic (slightly modified it in order for it to load my info the second ListView):
private class ItemHighlighterListener implements OnItemClickListener {
private View oldSelection = null;
public void clearSelection() {
if(oldSelection != null) {
oldSelection.setBackgroundColor(android.R.color.transparent);
}
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
clearSelection();
oldSelection = view;
view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.list_selector));
loadClubs(mXMLPortalOptions.getRegion(pos).getId());
mClubList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_white, mClubs));
}
}
Here's my list_selector.xml
file :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"><shape>
<solid android:color="#CCCCCC" />
</shape></item>
<item android:state_selected="false"><shape>
<solid android:color="#FFFFFF" />
</shape></item>
</selector>
The method (OnItemClick) is called and executed, but the background of my ListItem
stays the same color :/
I can't believe that this simple task has proven so complicated.
If I have omitted code that could be useful or if my question is lacking details, feel free to point that out and I'll do my best to explain myself.
question from:
https://stackoverflow.com/questions/9281000/android-keep-listviews-item-highlighted-once-one-has-been-clicked 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…