DefaultItemAnimator

class DefaultItemAnimator : 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 functions

Boolean

Called when an item is added to the RecyclerView.

Boolean
animateChange(
    oldHolder: RecyclerView.ViewHolder!,
    newHolder: RecyclerView.ViewHolder!,
    fromLeft: Int,
    fromTop: Int,
    toLeft: Int,
    toTop: Int
)

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(
    holder: RecyclerView.ViewHolder!,
    fromX: Int,
    fromY: Int,
    toX: Int,
    toY: Int
)

Called when an item is moved in the RecyclerView.

Boolean

Called when an item is removed from the RecyclerView.

Boolean
canReuseUpdatedViewHolder(
    viewHolder: RecyclerView.ViewHolder,
    payloads: (Mutable)List<Any!>
)

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).

Unit

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

Unit

Method called when all item animations should be ended immediately.

Boolean

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

Unit

Called when there are pending animations waiting to be started.

Inherited Constants

From androidx.recyclerview.widget.RecyclerView.ItemAnimator
const Int

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

const Int

The Item represented by this ViewHolder is updated.

const Int

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

const Int
FLAG_MOVED = 2048

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

const Int

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

Inherited functions

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

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

abstract Boolean
animateChange(
    oldHolder: RecyclerView.ViewHolder,
    newHolder: RecyclerView.ViewHolder,
    preLayoutInfo: RecyclerView.ItemAnimator.ItemHolderInfo,
    postLayoutInfo: RecyclerView.ItemAnimator.ItemHolderInfo
)

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).

Unit

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

Unit

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

Unit

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.

Boolean

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

RecyclerView.ItemAnimator.ItemHolderInfo

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

Unit

Called after dispatchAnimationFinished is called by the ItemAnimator.

Unit

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

RecyclerView.ItemAnimator.ItemHolderInfo

Called by the RecyclerView after the layout is complete.

RecyclerView.ItemAnimator.ItemHolderInfo
recordPreLayoutInformation(
    state: RecyclerView.State,
    viewHolder: RecyclerView.ViewHolder,
    @RecyclerView.ItemAnimator.AdapterChanges changeFlags: Int,
    payloads: (Mutable)List<Any!>
)

Called by the RecyclerView before the layout begins.

Unit
setAddDuration(addDuration: Long)

Sets the duration for which all add animations will run.

Unit
setChangeDuration(changeDuration: Long)

Sets the duration for which all change animations will run.

Unit
setMoveDuration(moveDuration: Long)

Sets the duration for which all move animations will run.

Unit
setRemoveDuration(removeDuration: Long)

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
animateChange(
    oldHolder: RecyclerView.ViewHolder,
    newHolder: RecyclerView.ViewHolder,
    preLayoutInfo: RecyclerView.ItemAnimator.ItemHolderInfo,
    postLayoutInfo: RecyclerView.ItemAnimator.ItemHolderInfo
)

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).

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Boolean

Returns whether this ItemAnimator supports animations of change events.

Unit

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

Unit

Called when an add animation is being started on the given ViewHolder.

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit
setSupportsChangeAnimations(supportsChangeAnimations: Boolean)

Sets whether this ItemAnimator supports animations of item change events.

Public constructors

DefaultItemAnimator

Added in 1.0.0
DefaultItemAnimator()

Public functions

animateAdd

Added in 1.4.0-alpha01
fun animateAdd(holder: RecyclerView.ViewHolder!): Boolean

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
fun animateChange(
    oldHolder: RecyclerView.ViewHolder!,
    newHolder: RecyclerView.ViewHolder!,
    fromLeft: Int,
    fromTop: Int,
    toLeft: Int,
    toTop: Int
): Boolean

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
fun animateMove(
    holder: RecyclerView.ViewHolder!,
    fromX: Int,
    fromY: Int,
    toX: Int,
    toY: Int
): Boolean

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
fun animateRemove(holder: RecyclerView.ViewHolder!): Boolean

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

fun canReuseUpdatedViewHolder(
    viewHolder: RecyclerView.ViewHolder,
    payloads: (Mutable)List<Any!>
): 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).

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
fun endAnimation(item: RecyclerView.ViewHolder!): Unit

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
fun endAnimations(): Unit

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
fun isRunning(): Boolean

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
fun runPendingAnimations(): Unit

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.