I have this animation issue in RecylcerView, in a ...
# random
u
I have this animation issue in RecylcerView, in a chat app, inserting a new message, then after it is sent, updating it (message.state changes). Insert animation plays okay however during when the update (notifyItemChanged(payload) comes, it just jumps to end position, stopping the animation
p
Disable the default item animator's change animation
u
Same thing unfortunately
fyi its because of this in DefaultItemAnimator
Copy code
@Override
    public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder,
            int fromX, int fromY, int toX, int toY) {
        if (oldHolder == newHolder) {
            // Don't know how to run change animations when the same view holder is re-used.
            // run a move animation to handle position changes.
            return animateMove(oldHolder, fromX, fromY, toX, toY);
        }
        final float prevTranslationX = oldHolder.itemView.getTranslationX();
        final float prevTranslationY = oldHolder.itemView.getTranslationY();
        final float prevAlpha = oldHolder.itemView.getAlpha();
        resetAnimation(oldHolder); <------------- THIS
p
Whats the call stack?
The javadoc of
setSupportsChangeAnimations
says:
Copy code
* @param supportsChangeAnimations true if change animations are supported by
     *                                 this ItemAnimator, false otherwise. If the property is false,
     *                                 the ItemAnimator
     *                                 will not receive a call to
     *                                 {@link #animateChange(RecyclerView.ViewHolder, RecyclerView.ViewHolder, int, int, int,
     *                                 int)} when changes occur.
So my suggestion should have fixed that
u
Yea javadoc sounds it should but it doesnt for some reason, unless im using it wrong
(recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
p
File a bug?
Copy code
fun RecyclerView.disableChangeAnimation() {
  val animator = itemAnimator
  if (animator is SimpleItemAnimator) {
    animator.supportsChangeAnimations = false
  }
}
u
btw now I tried to subclass the defaultitemanimator, and just dont call super on that animateChangeMethod and it works
p
Set a breakpoint and check where the call is coming from
u
when the flag is false?
p
Yes
Set a breakpoint to the first line of
animateChange
and check who called it in the callstack
u
image.png
only thing different is that with the flag false, it behaves as notifyItemChanged ( payload ) overload, i.e. oldHolder and newHolder are the same so it goes to animateMove, with not deltaX Y so it just calls endAnimation which cancels the translation animator from notifyItemInserted