android - Weird behavior of ListView item and state selector background drawable -



android - Weird behavior of ListView item and state selector background drawable -

i having unusual listview behavior when using statelistdrawable background. i've tried follow reply this post, wasn't getting state_checked state work, listview going crazy.

when click on item, doesn't alter color state_checked item in selector. after clicking around bit though, many of views switch state_checked background. it's seemingly random.

here state selector xml code:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <gradient android:startcolor="@color/grey" android:endcolor="@color/darkgrey" android:angle="270" /> <stroke android:width="0dp" android:color="@color/grey05" /> <corners android:radius="0dp" /> <padding android:left="10sp" android:top="10sp" android:right="10sp" android:bottom="10sp" /> </shape> </item> <item android:state_focused="true" > <shape> <gradient android:endcolor="@color/orange4" android:startcolor="@color/orange5" android:angle="270" /> <stroke android:width="0dp" android:color="@color/grey05" /> <corners android:radius="0dp" /> <padding android:left="10sp" android:top="10sp" android:right="10sp" android:bottom="10sp" /> </shape> </item> <item android:state_checked="true"> <shape> <gradient android:endcolor="@color/brown2" android:startcolor="@color/brown1" android:angle="270" /> <stroke android:width="0dp" android:color="@color/grey05" /> <corners android:radius="0dp" /> <padding android:left="10sp" android:top="10sp" android:right="10sp" android:bottom="10sp" /> </shape> </item> <item android:state_selected="true"> <shape> <gradient android:endcolor="@color/brown2" android:startcolor="@color/brown1" android:angle="270" /> <stroke android:width="0dp" android:color="@color/grey05" /> <corners android:radius="0dp" /> <padding android:left="10sp" android:top="10sp" android:right="10sp" android:bottom="10sp" /> </shape> </item> <item> <shape> <gradient android:startcolor="@color/white" android:endcolor="@color/white2" android:angle="270" /> <stroke android:width="0dp" android:color="@color/grey05" /> <corners android:radius="0dp" /> <padding android:left="10sp" android:top="10sp" android:right="10sp" android:bottom="10sp" /> </shape> </item> </selector>

and here .java class custom view implementing checkable:

public class entry extends linearlayout implements checkable { public entry(context context) { super(context, null); // inflate view layoutinflater temp = (layoutinflater)context.getsystemservice(context.layout_inflater_service); temp.inflate(r.layout.entry, this, true); initviews(); } private static final int[] checkedstateset = { android.r.attr.state_checked }; private void initviews() { this.setbackgroundresource(r.drawable.listview_row); } public boolean ischecked() { homecoming _checked; } public void toggle() { _checked = !_checked; } public void setchecked(boolean checked) { _checked = checked; } @override protected int[] oncreatedrawablestate(int extraspace) { final int[] drawablestate = super.oncreatedrawablestate(extraspace + 1); if (ischecked()) { mergedrawablestates(drawablestate, checkedstateset); } homecoming drawablestate; } @override public boolean performclick() { toggle(); homecoming super.performclick(); } }

i've poked around few hours trying figure out, unfortunately must concede asking help. can see wrong code above cause listview behave strangely on items? can post more code well, if needed.

when working listview of import maintain in mind views presentation , adapter data model.

this means of state should in adapter (the info model), not in views.

from can tell of code, have view showing check state, , state in view not in adapter. is, when user clicks on item in list, view beingness used display item has internal checked state changed toggle shown user.

but since view not info model, state playing here transient , not associated adapter item beingness clicked.

the obvious problem causes comes in view recycling. when scroll through listview, when items scroll off end , new ones appear @ bottom, views used display old items re-used display new ones. much more efficient having inflate new item view hierarchy every time new item shown.

because have state in view, when recycling happens state in view gets randomly re-associated new item. can happen in many cases, not scrolling.

the solution set check state in adapter, , implement adapter.getview() set checked state of view based on state have in adapter. way whenever view recycled (and getview() called bind new info row it), update checked state correctly follow new info displaying.

android listview drawable

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -