DefaultItemAnimator

public class DefaultItemAnimator extends SimpleItemAnimator


This implementation of RecyclerView.ItemAnimator provides basic animations on remove, add, and move events that happen to the items in a RecyclerView. RecyclerView uses a DefaultItemAnimator by default.

See also
setItemAnimator

Summary

Public constructors

Public methods

boolean

Called when an item is added to the RecyclerView.

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 RecyclerView.Adapter#notifyItemChanged(int) or RecyclerView.Adapter#notifyItemRangeChanged(int, int).

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

Called when an item is moved in the RecyclerView.

boolean

Called when an item is removed from the RecyclerView.

boolean

When an item is changed, ItemAnimator can decide whether it wants to re-use the same ViewHolder for animations or RecyclerView should create a copy of the item and ItemAnimator will use both to run the animation (e.g. cross-fade).

void

Method called when an animation on a view should be ended immediately.

void

Method called when all item animations should be ended immediately.

boolean

Method which returns whether there are any item animations currently running.

void

Called when there are pending animations waiting to be started.

Inherited Constants

From androidx.recyclerview.widget.RecyclerView.ItemAnimator
static final int

This ViewHolder was not laid out but has been added to the layout in pre-layout state by the LayoutManager.

static final int

The Item represented by this ViewHolder is updated.

static final int

Adapter notifyDataSetChanged has been called and the content represented by this ViewHolder is invalid.

static final int
FLAG_MOVED = 2048

The position of the Item represented by this ViewHolder has been changed.

static final int

The Item represented by this ViewHolder is removed from the adapter.

Inherited methods

From androidx.recyclerview.widget.RecyclerView.ItemAnimator
abstract boolean

Called by the RecyclerView when a ViewHolder is added to the layout.

abstract boolean

Called by the RecyclerView when an adapter item is present both before and after the layout and RecyclerView has received a notifyItemChanged call for it.

abstract boolean

Called by the RecyclerView when a ViewHolder has disappeared from the layout.

abstract boolean

Called by the RecyclerView when a ViewHolder is present in both before and after the layout and RecyclerView has not received a notifyItemChanged call for it or a notifyDataSetChanged call.

boolean

When an item is changed, ItemAnimator can decide whether it wants to re-use the same ViewHolder for animations or RecyclerView should create a copy of the item and ItemAnimator will use both to run the animation (e.g. cross-fade).

final void

Method to be called by subclasses when an animation is finished.

final void

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

final void

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

long

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

long

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

long

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

long

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

final boolean

Like isRunning, this method returns whether there are any item animations currently running.

@NonNull RecyclerView.ItemAnimator.ItemHolderInfo

Returns a new ItemHolderInfo which will be used to store information about the ViewHolder.

void

Called after dispatchAnimationFinished is called by the ItemAnimator.

void

Called when a new animation is started on the given ViewHolder.

@NonNull RecyclerView.ItemAnimator.ItemHolderInfo

Called by the RecyclerView after the layout is complete.

@NonNull RecyclerView.ItemAnimator.ItemHolderInfo
recordPreLayoutInformation(
    @NonNull RecyclerView.State state,
    @NonNull RecyclerView.ViewHolder viewHolder,
    @RecyclerView.ItemAnimator.AdapterChanges int changeFlags,
    @NonNull List<Object> payloads
)

Called by the RecyclerView before the layout begins.

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.

From androidx.recyclerview.widget.SimpleItemAnimator
boolean

Called by the RecyclerView when a ViewHolder is added to the layout.

boolean

Called by the RecyclerView when an adapter item is present both before and after the layout and RecyclerView has received a notifyItemChanged call for it.

boolean

Called by the RecyclerView when a ViewHolder has disappeared from the layout.

boolean

Called by the RecyclerView when a ViewHolder is present in both before and after the layout and RecyclerView has not received a notifyItemChanged call for it or a notifyDataSetChanged call.

boolean

When an item is changed, ItemAnimator can decide whether it wants to re-use the same ViewHolder for animations or RecyclerView should create a copy of the item and ItemAnimator will use both to run the animation (e.g. cross-fade).

final void

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

final void

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

final void

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

final void

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

final void

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

final void

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

final void

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

final void

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

boolean

Returns whether this ItemAnimator supports animations of change events.

void

Called when an add animation has ended on the given ViewHolder.

void

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

Called when a move animation has ended on the given ViewHolder.

void

Called when a move animation is being started on the given ViewHolder.

void

Called when a remove animation has ended on the given ViewHolder.

void

Called when a remove animation is being started on the given ViewHolder.

void
setSupportsChangeAnimations(boolean supportsChangeAnimations)

Sets whether this ItemAnimator supports animations of item change events.

Public constructors

DefaultItemAnimator

Added in 1.0.0
public DefaultItemAnimator()

Public methods

animateAdd

Added in 1.4.0-alpha01
public 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(RecyclerView.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(RecyclerView.ViewHolder) animateAdd(), #animateMove(RecyclerView.ViewHolder, int, int, int, int) animateMove(), #animateRemove(RecyclerView.ViewHolder) animateRemove(), and #animateChange(RecyclerView.ViewHolder, RecyclerView.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.

animateChange

Added in 1.4.0-alpha01
public 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 RecyclerView.Adapter#notifyItemChanged(int) or RecyclerView.Adapter#notifyItemRangeChanged(int, int).

Implementers can choose whether and how to animate changes, but must always call #dispatchChangeFinished(RecyclerView.ViewHolder, boolean) for each non-null distinct ViewHolder, either immediately (if no animation will occur) or after the animation actually finishes. If the oldHolder} is the same ViewHolder as the newHolder}, you must call #dispatchChangeFinished(RecyclerView.ViewHolder, boolean) once and only once. In that case, the second parameter of dispatchChangeFinished} is ignored.

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(RecyclerView.ViewHolder) animateAdd(), #animateMove(RecyclerView.ViewHolder, int, int, int, int) animateMove(), #animateRemove(RecyclerView.ViewHolder) animateRemove(), and #animateChange(RecyclerView.ViewHolder, RecyclerView.ViewHolder, int, int, int, int) come in one by one, then start the animations together in the later call to #runPendingAnimations().

animateMove

Added in 1.4.0-alpha01
public 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(RecyclerView.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(RecyclerView.ViewHolder) animateAdd(), #animateMove(RecyclerView.ViewHolder, int, int, int, int) animateMove(), #animateRemove(RecyclerView.ViewHolder) animateRemove(), and #animateChange(RecyclerView.ViewHolder, RecyclerView.ViewHolder, int, int, int, int) come in one by one, then start the animations together in the later call to #runPendingAnimations().

animateRemove

Added in 1.4.0-alpha01
public 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(RecyclerView.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(RecyclerView.ViewHolder) animateAdd(), #animateMove(RecyclerView.ViewHolder, int, int, int, int) animateMove(), #animateRemove(RecyclerView.ViewHolder) animateRemove(), and #animateChange(RecyclerView.ViewHolder, RecyclerView.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.

canReuseUpdatedViewHolder

public boolean canReuseUpdatedViewHolder(
    @NonNull RecyclerView.ViewHolder viewHolder,
    @NonNull List<Object> payloads
)

When an item is changed, ItemAnimator can decide whether it wants to re-use the same ViewHolder for animations or RecyclerView should create a copy of the item and ItemAnimator will use both to run the animation (e.g. cross-fade).

Note that this method will only be called if the still has the same type ( Adapter#getItemViewType(int)). Otherwise, ItemAnimator will always receive both s in the #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) method.

If the payload list is not empty, DefaultItemAnimator returns true. When this is the case:

  • If you override animateChange, both ViewHolder arguments will be the same instance.
  • If you are not overriding animateChange, then DefaultItemAnimator will call animateMove and run a move animation instead.

endAnimation

Added in 1.4.0-alpha01
public 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, #dispatchAnimationFinished(ViewHolder) should be called for each finished animation since the animations are effectively done when this method is called.

endAnimations

Added in 1.4.0-alpha01
public 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, #dispatchAnimationFinished(ViewHolder) should be called for each finished animation since the animations are effectively done when this method is called.

isRunning

Added in 1.4.0-alpha01
public 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.

runPendingAnimations

Added in 1.4.0-alpha01
public void runPendingAnimations()

Called when there are pending animations waiting to be started. This state is governed by the return values from #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) animateAppearance(), #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) animateChange() #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) animatePersistence(), and #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) animateDisappearance(), 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.