0

I have a standard unaltered Switch class on every list item in a RecyclerView (in order to toggle an item on or off). Usually switches in Android have a nice default animation with the switch sliding into position and a small bubble animation appears. In my case, the switch just jumps into the final position without any animation. Here is my layout for it:

<Switch
    android:id="@+id/alarm_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:checked="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:animateLayoutChanges="true"
    android:padding="10dp" />

The property android:animateLayoutChanges="true" does not have any effect. Tried on Android M and O. How can I enable the standard animation?

Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33
  • Does toggling the switch trigger any java code (especially anything that would call e.g. `notifyDataSetChanged()` or similar)? – Ben P. Nov 08 '17 at 10:06
  • In fact, yes, there is a long-running operation that is run in an AsyncTask, but apparently it is blocking the UI thread somehow. I commented out the code, and the animation is working. Please post an answer, and I will accept it. Thanks! – Dmitri Borohhov Nov 08 '17 at 10:16

1 Answers1

0

If toggling your Switch executes any java code, it's possible for this to affect whether or not animations play. Perhaps you have something that calls notifyDataSetChanged() (or similar), which will cause an immediate re-draw of the entire RecyclerView. Or perhaps you're accidentally blocking the UI thread, which would prevent animations of any sort.

Ben P.
  • 52,661
  • 6
  • 95
  • 123