I'm trying to get my app working properly in 2.3 (it works fine in 4.0+) and one issue I'm having is that on my listview I can't get the background on the selected item to change. I'm not sure what I need to change - does anyone know?
Here's the listview itself:
<ListView
android:id="@+id/score_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/keyboard"
android:layout_below="@+id/sort_header"
android:choiceMode="singleChoice"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:listSelector="@drawable/selector" />
Here's the selector that works in 4.0+ (in 2.3 there is no color change):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/highlight"/>
<item android:state_pressed="true" android:drawable="@color/highlight"/>
<item android:state_activated="true" android:drawable="@color/highlight"/>
<item android:state_selected="true" android:drawable="@color/highlight"/>
</selector>
I actually don't need all 4 of those, but I wanted to try everything.
I had exactly the same problem. I haven't found a way how to do this in XML, but I have found a workaround in code. The following code is tested in application that supports API level 7+
First, you need to alter the adapter for the ListView
a bit:
public class ListViewAdapter extends BaseAdapter {
private int selectedItemPosition = -1;
// your code
public void selectItem(int i) {
selectedItemPosition = i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// your code
if (i == selectedItemPosition) {
// set the desired background color
textView.setBackgroundColor(context.getResources().getColor(R.color.highlight));
}
else {
// set the default (not selected) background color to a transparent color (or any other)
textView.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
}
Next you have to inform the adapter that the selection changed in your OnItemClickListener
's onItemClickMethod
:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// select the item
((ListViewAdapter) listview.getAdapter()).selectItem(position);
// force redraw of the listview (invalidating just "view" is not enough)
((ListView) parent).invalidateViews();
// your code
}
That should be it. Now whenever you wan't to change the selected item you can use the same code that is used in onItemClick()
, ie. selectItem()
followed by invalidateViews()
. Instead of calling invalidateViews()
, the adapter's notifyDataSetChanged()
can also be used.
Also you should also add an appropriate listSelector to the list view, to avoid the brief flicker of the default selector when the item is clicked. However there's a bug with list selectors on API 7 and 8 when the background of the whole view is changed. You can find a workaround here
Try to set property in your listview android:cacheColorHint="@null
. The listview background won't be hightlight by touch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With