I want each even item in the ListView
to be of different color. For this purpose I used the following code :
public void bindView(View row, Context ctxt, Cursor c) {
ViewHolder holder = (ViewHolder) row.getTag();
if (c.getPosition() % 2 == 0) {
row.setBackgroundDrawable(getResources().getDrawable(
R.drawable.dark_item_background));
}
holder.populateFrom(c, mDbHelper);
}
But the item isn't highlighted after the touch event. Please advice how to overcome this.
Update
As Gophermofur advice I created a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_activated_holo" android:state_activated="true"/>
<item android:drawable="@color/item_back_color"/>
</selector>
Now it works on Jelly Bean and does not on Gingerbread. Any thoughts?
Items in a listview aren't actually defined as solid colours (black or white), they are defined using selectors where there are multiple colours for different states (normal = black, pressed = orange, focused = blue, etc).
When you start replacing the background colour of items in the listview with a solid colour, it overrides that selector so there are no longer different colours for different states.
Normally a listview selector is applied to the entire listivew, however, you may be able to apply it directly to an item's background or selector (I'm not sure and I can't test at the moment).
Take a look at these links to see how to create a selector XML file:
Now it works on Jelly Bean and does not on Gingerbread. Any thoughts?
I'm not sure what your drawable/list_activated_holo is, but by the name of it it sounds like its using the android Holo theme - which was only added in API level 14. http://developer.android.com/design/style/themes.html
You'll need to define the colours in your own values/colors.xml if you want to use them on devices running less than API level 14.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.