DefaultItemAnimator
class DefaultItemAnimator : SimpleItemAnimator
kotlin.Any | |||
↳ | androidx.recyclerview.widget.RecyclerView.ItemAnimator | ||
↳ | androidx.recyclerview.widget.SimpleItemAnimator | ||
↳ | androidx.recyclerview.widget.DefaultItemAnimator |
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 |
animateAdd(holder: RecyclerView.ViewHolder!) Called when an item is added to the RecyclerView. |
Boolean |
animateChange( 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( Called when an item is moved in the RecyclerView. |
Boolean |
animateRemove(holder: RecyclerView.ViewHolder!) Called when an item is removed from the RecyclerView. |
Boolean |
canReuseUpdatedViewHolder( 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 |
endAnimation(item: RecyclerView.ViewHolder!) 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 |
||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
Inherited functions |
||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
Public functions
animateAdd
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
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
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
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 callanimateMove
and run a move animation instead.
endAnimation
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
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
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
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.