alex alex - 2 months ago 18
Android Question

Change Switch state without animation

In my Android project, I have a

ListView
with rows containing
SwitchCompat
items (AppCompat for
Switch
widget).

My problem occurs when I scroll into the list and
getView(...)
method of
MyAdapter
is invoked with a
recycled
view. I redefine the correct
Switch
state but the animation is visible.

There is a solution to prevent the animation in this case?

enter image description here

Answer

I finally found a solution but seems not really clean:

ViewGroup viewGroup = (ViewGroup) view; // the recycled view
viewGroup.removeView(switch);
switch.setChecked(states[index]);
viewGroup.addView(switch);

If a better solution exists, please share it.