java android listView set diffrent background color and selected item -
i want set diffrent background color in lit view in adapter did
if (position % 2 == 0) { convertview.setbackgroundcolor(context.getresources().getcolor(r.color.darkgrey)); } else { convertview.setbackgroundcolor(context.getresources().getcolor(r.color.grey)); }
and works when added :
android:listselector="@drawable/list_selector"
here :
<listview android:id="@+id/list" android:layout_margintop="5dp" android:layout_width="fill_parent" android:layout_height="match_parent" android:drawselectorontop="false" android:scrollbarthumbvertical="@drawable/scrollbar_vertical_thumb" android:visibility="visible" android:listselector="@drawable/list_selector" android:layout_above="@+id/bottompanel" />
i didn't see selected item when remove setting color in adapter see selected item
> these following steps show. step1.1) use 2 selector odd , postion list item
artists_list_backgroundcolor.xml
<?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/grey" /> <item android:state_pressed="true" android:drawable="@color/itemselected" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/itemselected" /> </selector>
step 1.2) artists_list_background_alternate.xml
<?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/sign_out_color" /> <item android:state_pressed="true" android:drawable="@color/login_hover" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/login_hover" /> </selector>
step2) colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="survey_toplist_item">#efedec</color> <color name="survey_alternate_color">#ebe7e6</color> <color name="grey">#ffffff</color> <color name="itemselected">#ededed</color> <color name="login_hover">#e5f5fa</color> <color name="sign_out_color">#e84040</color> </resources>
step 3) in arrayadapter:
@override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if (view == null) { view = linflater.inflate(r.layout.listitem, parent, false); } if (position % 2 == 0) { view.setbackgroundresource(r.drawable.artists_list_backgroundcolor); } else { view.setbackgroundresource(r.drawable.artists_list_background_alternate); } ((textview) view.findviewbyid(r.id.heading)).settext(data.get(position)); return view; }
for more details go through belog link
http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html
Comments
Post a Comment