Android APIs
public static abstract class

RecyclerView.ItemAnimator

extends Object
java.lang.Object
   ↳ android.support.v7.widget.RecyclerView.ItemAnimator
Known Direct Subclasses

Class Overview

This class defines the animations that take place on items as changes are made to the adapter. Subclasses of ItemAnimator can be used to implement custom animations for actions on ViewHolder items. The RecyclerView will manage retaining these items while they are being animated, but implementors must call the appropriate "Starting" (dispatchRemoveStarting(ViewHolder), dispatchMoveStarting(ViewHolder), dispatchChangeStarting(ViewHolder, boolean), or dispatchAddStarting(ViewHolder)) and "Finished" (dispatchRemoveFinished(ViewHolder), dispatchMoveFinished(ViewHolder), dispatchChangeFinished(ViewHolder, boolean), or dispatchAddFinished(ViewHolder)) methods when each item animation is being started and ended.

By default, RecyclerView uses DefaultItemAnimator

Summary

Nested Classes
interface RecyclerView.ItemAnimator.ItemAnimatorFinishedListener This interface is used to inform listeners when all pending or running animations in an ItemAnimator are finished. 
Public Constructors
RecyclerView.ItemAnimator()
Public Methods
abstract boolean animateAdd(RecyclerView.ViewHolder holder)
Called when an item is added to the RecyclerView.
abstract boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop)
Called when an item is changed in the RecyclerView, as indicated by a call to notifyItemChanged(int) or notifyItemRangeChanged(int, int).
abstract boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY)
Called when an item is moved in the RecyclerView.
abstract boolean animateRemove(RecyclerView.ViewHolder holder)
Called when an item is removed from the RecyclerView.
final void dispatchAddFinished(RecyclerView.ViewHolder item)
Method to be called by subclasses when an add animation is done.
final void dispatchAddStarting(RecyclerView.ViewHolder item)
Method to be called by subclasses when an add animation is being started.
final void dispatchAnimationsFinished()
This method should be called by ItemAnimator implementations to notify any listeners that all pending and active item animations are finished.
final void dispatchChangeFinished(RecyclerView.ViewHolder item, boolean oldItem)
Method to be called by subclasses when a change animation is done.
final void dispatchChangeStarting(RecyclerView.ViewHolder item, boolean oldItem)
Method to be called by subclasses when a change animation is being started.
final void dispatchMoveFinished(RecyclerView.ViewHolder item)
Method to be called by subclasses when a move animation is done.
final void dispatchMoveStarting(RecyclerView.ViewHolder item)
Method to be called by subclasses when a move animation is being started.
final void dispatchRemoveFinished(RecyclerView.ViewHolder item)
Method to be called by subclasses when a remove animation is done.
final void dispatchRemoveStarting(RecyclerView.ViewHolder item)
Method to be called by subclasses when a remove animation is being started.
abstract void endAnimation(RecyclerView.ViewHolder item)
Method called when an animation on a view should be ended immediately.
abstract void endAnimations()
Method called when all item animations should be ended immediately.
long getAddDuration()
Gets the current duration for which all add animations will run.
long getChangeDuration()
Gets the current duration for which all change animations will run.
long getMoveDuration()
Gets the current duration for which all move animations will run.
long getRemoveDuration()
Gets the current duration for which all remove animations will run.
boolean getSupportsChangeAnimations()
Returns whether this ItemAnimator supports animations of change events.
final boolean isRunning(RecyclerView.ItemAnimator.ItemAnimatorFinishedListener listener)
Like isRunning(), this method returns whether there are any item animations currently running.
abstract boolean isRunning()
Method which returns whether there are any item animations currently running.
void onAddFinished(RecyclerView.ViewHolder item)
Called when an add animation has ended on the given ViewHolder.
void onAddStarting(RecyclerView.ViewHolder item)
Called when an add animation is being started on the given ViewHolder.
void onChangeFinished(RecyclerView.ViewHolder item, boolean oldItem)
Called when a change animation has ended on the given ViewHolder.
void onChangeStarting(RecyclerView.ViewHolder item, boolean oldItem)
Called when a change animation is being started on the given ViewHolder.
void onMoveFinished(RecyclerView.ViewHolder item)
Called when a move animation has ended on the given ViewHolder.
void onMoveStarting(RecyclerView.ViewHolder item)
Called when a move animation is being started on the given ViewHolder.
void onRemoveFinished(RecyclerView.ViewHolder item)
Called when a remove animation has ended on the given ViewHolder.
void onRemoveStarting(RecyclerView.ViewHolder item)
Called when a remove animation is being started on the given ViewHolder.
abstract void runPendingAnimations()
Called when there are pending animations waiting to be started.
void setAddDuration(long addDuration)
Sets the duration for which all add animations will run.
void setChangeDuration(long changeDuration)
Sets the duration for which all change animations will run.
void setMoveDuration(long moveDuration)
Sets the duration for which all move animations will run.
void setRemoveDuration(long removeDuration)
Sets the duration for which all remove animations will run.
void setSupportsChangeAnimations(boolean supportsChangeAnimations)
Sets whether this ItemAnimator supports animations of item change events.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public RecyclerView.ItemAnimator ()

Public Methods

public abstract boolean animateAdd (RecyclerView.ViewHolder holder)

Called when an item is added to the RecyclerView. Implementors can choose whether and how to animate that change, but must always call dispatchAddFinished(ViewHolder) when done, either immediately (if no animation will occur) or after the animation actually finishes. The return value indicates whether an animation has been set up and whether the ItemAnimator's runPendingAnimations() method should be called at the next opportunity. This mechanism allows ItemAnimator to set up individual animations as separate calls to animateAdd(), animateMove(), animateRemove(), and animateChange(ViewHolder, ViewHolder, int, int, int, int) come in one by one, then start the animations together in the later call to runPendingAnimations().

This method may also be called for appearing items which were already in the RecyclerView, but for which the system does not have enough information to animate them into view. In that case, the default animation for adding items is run on those items as well.

Parameters
holder The item that is being added.
Returns

public abstract boolean animateChange (RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop)

Called when an item is changed in the RecyclerView, as indicated by a call to notifyItemChanged(int) or notifyItemRangeChanged(int, int).

Implementers can choose whether and how to animate changes, but must always call dispatchChangeFinished(ViewHolder, boolean) for each non-null ViewHolder, either immediately (if no animation will occur) or after the animation actually finishes. The return value indicates whether an animation has been set up and whether the ItemAnimator's runPendingAnimations() method should be called at the next opportunity. This mechanism allows ItemAnimator to set up individual animations as separate calls to animateAdd(), animateMove(), animateRemove(), and animateChange(ViewHolder, ViewHolder, int, int, int, int) come in one by one, then start the animations together in the later call to runPendingAnimations().

Parameters
oldHolder The original item that changed.
newHolder The new item that was created with the changed content. Might be null
fromLeft Left of the old view holder
fromTop Top of the old view holder
toLeft Left of the new view holder
toTop Top of the new view holder
Returns

public abstract boolean animateMove (RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY)

Called when an item is moved in the RecyclerView. Implementors can choose whether and how to animate that change, but must always call dispatchMoveFinished(ViewHolder) when done, either immediately (if no animation will occur) or after the animation actually finishes. The return value indicates whether an animation has been set up and whether the ItemAnimator's runPendingAnimations() method should be called at the next opportunity. This mechanism allows ItemAnimator to set up individual animations as separate calls to animateAdd(), animateMove(), animateRemove(), and animateChange(ViewHolder, ViewHolder, int, int, int, int) come in one by one, then start the animations together in the later call to runPendingAnimations().

Parameters
holder The item that is being moved.
Returns

public abstract boolean animateRemove (RecyclerView.ViewHolder holder)

Called when an item is removed from the RecyclerView. Implementors can choose whether and how to animate that change, but must always call dispatchRemoveFinished(ViewHolder) when done, either immediately (if no animation will occur) or after the animation actually finishes. The return value indicates whether an animation has been set up and whether the ItemAnimator's runPendingAnimations() method should be called at the next opportunity. This mechanism allows ItemAnimator to set up individual animations as separate calls to animateAdd(), animateMove(), animateRemove(), and animateChange(ViewHolder, ViewHolder, int, int, int, int) come in one by one, then start the animations together in the later call to runPendingAnimations().

This method may also be called for disappearing items which continue to exist in the RecyclerView, but for which the system does not have enough information to animate them out of view. In that case, the default animation for removing items is run on those items as well.

Parameters
holder The item that is being removed.
Returns

public final void dispatchAddFinished (RecyclerView.ViewHolder item)

Method to be called by subclasses when an add animation is done.

Parameters
item The item which has been added

public final void dispatchAddStarting (RecyclerView.ViewHolder item)

Method to be called by subclasses when an add animation is being started.

Parameters
item The item being added

public final void dispatchAnimationsFinished ()

This method should be called by ItemAnimator implementations to notify any listeners that all pending and active item animations are finished.

public final void dispatchChangeFinished (RecyclerView.ViewHolder item, boolean oldItem)

Method to be called by subclasses when a change animation is done.

Parameters
item The item which has been changed (this method must be called for each non-null ViewHolder passed into animateChange(ViewHolder, ViewHolder, int, int, int, int)).
oldItem true if this is the old item that was changed, false if it is the new item that replaced the old item.

public final void dispatchChangeStarting (RecyclerView.ViewHolder item, boolean oldItem)

Method to be called by subclasses when a change animation is being started.

Parameters
item The item which has been changed (this method must be called for each non-null ViewHolder passed into animateChange(ViewHolder, ViewHolder, int, int, int, int)).
oldItem true if this is the old item that was changed, false if it is the new item that replaced the old item.

public final void dispatchMoveFinished (RecyclerView.ViewHolder item)

Method to be called by subclasses when a move animation is done.

Parameters
item The item which has been moved

public final void dispatchMoveStarting (RecyclerView.ViewHolder item)

Method to be called by subclasses when a move animation is being started.

Parameters
item The item being moved

public final void dispatchRemoveFinished (RecyclerView.ViewHolder item)

Method to be called by subclasses when a remove animation is done.

Parameters
item The item which has been removed

public final void dispatchRemoveStarting (RecyclerView.ViewHolder item)

Method to be called by subclasses when a remove animation is being started.

Parameters
item The item being removed

public abstract void endAnimation (RecyclerView.ViewHolder item)

Method called when an animation on a view should be ended immediately. This could happen when other events, like scrolling, occur, so that animating views can be quickly put into their proper end locations. Implementations should ensure that any animations running on the item are canceled and affected properties are set to their end values. Also, appropriate dispatch methods (e.g., dispatchAddFinished(ViewHolder) should be called since the animations are effectively done when this method is called.

Parameters
item The item for which an animation should be stopped.

public abstract void endAnimations ()

Method called when all item animations should be ended immediately. This could happen when other events, like scrolling, occur, so that animating views can be quickly put into their proper end locations. Implementations should ensure that any animations running on any items are canceled and affected properties are set to their end values. Also, appropriate dispatch methods (e.g., dispatchAddFinished(ViewHolder) should be called since the animations are effectively done when this method is called.

public long getAddDuration ()

Gets the current duration for which all add animations will run.

Returns
  • The current add duration

public long getChangeDuration ()

Gets the current duration for which all change animations will run.

Returns
  • The current change duration

public long getMoveDuration ()

Gets the current duration for which all move animations will run.

Returns
  • The current move duration

public long getRemoveDuration ()

Gets the current duration for which all remove animations will run.

Returns
  • The current remove duration

public boolean getSupportsChangeAnimations ()

Returns whether this ItemAnimator supports animations of change events.

Returns
  • true if change animations are supported, false otherwise

public final boolean isRunning (RecyclerView.ItemAnimator.ItemAnimatorFinishedListener listener)

Like isRunning(), this method returns whether there are any item animations currently running. Addtionally, the listener passed in will be called when there are no item animations running, either immediately (before the method returns) if no animations are currently running, or when the currently running animations are finished.

Note that the listener is transient - it is either called immediately and not stored at all, or stored only until it is called when running animations are finished sometime later.

Parameters
listener A listener to be called immediately if no animations are running or later when currently-running animations have finished. A null listener is equivalent to calling isRunning().
Returns
  • true if there are any item animations currently running, false otherwise.

public abstract boolean isRunning ()

Method which returns whether there are any item animations currently running. This method can be used to determine whether to delay other actions until animations end.

Returns
  • true if there are any item animations currently running, false otherwise.

public void onAddFinished (RecyclerView.ViewHolder item)

Called when an add animation has ended on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.

public void onAddStarting (RecyclerView.ViewHolder item)

Called when an add animation is being started on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.

public void onChangeFinished (RecyclerView.ViewHolder item, boolean oldItem)

Called when a change animation has ended on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.
oldItem true if this is the old item that was changed, false if it is the new item that replaced the old item.

public void onChangeStarting (RecyclerView.ViewHolder item, boolean oldItem)

Called when a change animation is being started on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.
oldItem true if this is the old item that was changed, false if it is the new item that replaced the old item.

public void onMoveFinished (RecyclerView.ViewHolder item)

Called when a move animation has ended on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.

public void onMoveStarting (RecyclerView.ViewHolder item)

Called when a move animation is being started on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.

public void onRemoveFinished (RecyclerView.ViewHolder item)

Called when a remove animation has ended on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.

public void onRemoveStarting (RecyclerView.ViewHolder item)

Called when a remove animation is being started on the given ViewHolder. The default implementation does nothing. Subclasses may wish to override this method to handle any ViewHolder-specific operations linked to animation lifecycles.

Parameters
item The ViewHolder being animated.

public abstract void runPendingAnimations ()

Called when there are pending animations waiting to be started. This state is governed by the return values from animateAdd(), animateMove(), and animateRemove(), which inform the RecyclerView that the ItemAnimator wants to be called later to start the associated animations. runPendingAnimations() will be scheduled to be run on the next frame.

public void setAddDuration (long addDuration)

Sets the duration for which all add animations will run.

Parameters
addDuration The add duration

public void setChangeDuration (long changeDuration)

Sets the duration for which all change animations will run.

Parameters
changeDuration The change duration

public void setMoveDuration (long moveDuration)

Sets the duration for which all move animations will run.

Parameters
moveDuration The move duration

public void setRemoveDuration (long removeDuration)

Sets the duration for which all remove animations will run.

Parameters
removeDuration The remove duration

public void setSupportsChangeAnimations (boolean supportsChangeAnimations)

Sets whether this ItemAnimator supports animations of item change events. If you set this property to false, actions on the data set which change the contents of items will not be animated. What those animations are is left up to the discretion of the ItemAnimator subclass, in its animateChange(ViewHolder, ViewHolder, int, int, int, int) implementation. The value of this property is true by default.

Parameters
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 animateChange(ViewHolder, ViewHolder, int, int, int, int) when changes occur.