android listview alternate row color BUT with default cursor selection -
i have been on web, stackoverflow included , can't seem clear complete way
i want create listview that
1) has alternating colors (i able code below) 2) retains default orange selection behavior of android
to accomplish #1 have custom adapter extends arrayadapter , override getview so
public view getview(int position, view convertview, viewgroup parent) { .... // tablelayoutid id pointing each view/row in list view tablelayoutview = view.findviewbyid(r.id.tablelayoutid); if(tablelayoutview != null) { int colorpos = position % colors.length; tablelayoutview.setbackgroundcolor(colors[colorpos]); } }
my member variable colors is
private int[] colors = new int[] { 0x30ffffff, 0x30ff2020, 0x30808080 };
followed article "android – applying alternate row color in listview simpleadapter" found here
now stuck, see on stackoverflow mention of doing see common, , suggest adding attribute
android:listselector="@color/list_item"
where list_item.xml like
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/transparent" /> ..... </selector>
then have add code getview() figure out state in , act accordingly.
is there example out there getting work? i'll gladly post mine use if work. :-(
a workaround use 2 selectors. adapter, instead of setting 2 colors, set 2 selectors.
if (position % 2 == 0) { view.setbackgroundresource(r.drawable.selector_1); } else { view.setbackgroundresource(r.drawable.selector_2); }
selector_1 defined in selector_1.xml this:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:state_pressed="false" android:drawable="@color/white" /> <item android:state_pressed="true" android:drawable="@color/orange" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/orange" /> </selector>
selector_2 defined in selector_2.xml this:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:state_pressed="false" android:drawable="@color/violet" /> <item android:state_pressed="true" android:drawable="@color/orange" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/orange" /> </selector>
so that, have bi-color listview , third color/shape/whatever-you-want selected item.
Comments
Post a Comment