public class DefaultItemAnimator extends SimpleItemAnimator
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.RecyclerView.ItemAnimator.AdapterChanges, RecyclerView.ItemAnimator.ItemAnimatorFinishedListener, RecyclerView.ItemAnimator.ItemHolderInfo
FLAG_APPEARED_IN_PRE_LAYOUT, FLAG_CHANGED, FLAG_INVALIDATED, FLAG_MOVED, FLAG_REMOVED
Constructor and Description |
---|
DefaultItemAnimator() |
Modifier and Type | Method and Description |
---|---|
boolean |
animateAdd(RecyclerView.ViewHolder holder)
Called when an item is added to the RecyclerView.
|
boolean |
animateChange(RecyclerView.ViewHolder oldHolder,
RecyclerView.ViewHolder newHolder,
int fromX,
int fromY,
int toX,
int toY)
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 |
animateRemove(RecyclerView.ViewHolder holder)
Called when an item is removed from the RecyclerView.
|
boolean |
canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder,
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).
|
void |
endAnimation(RecyclerView.ViewHolder item)
Method called when an animation on a view should be ended immediately.
|
void |
endAnimations()
Method called when all item animations should be ended immediately.
|
boolean |
isRunning()
Method which returns whether there are any item animations currently running.
|
void |
runPendingAnimations()
Called when there are pending animations waiting to be started.
|
animateAppearance, animateChange, animateDisappearance, animatePersistence, canReuseUpdatedViewHolder, dispatchAddFinished, dispatchAddStarting, dispatchChangeFinished, dispatchChangeStarting, dispatchMoveFinished, dispatchMoveStarting, dispatchRemoveFinished, dispatchRemoveStarting, getSupportsChangeAnimations, onAddFinished, onAddStarting, onChangeFinished, onChangeStarting, onMoveFinished, onMoveStarting, onRemoveFinished, onRemoveStarting, setSupportsChangeAnimations
dispatchAnimationFinished, dispatchAnimationsFinished, dispatchAnimationStarted, getAddDuration, getChangeDuration, getMoveDuration, getRemoveDuration, isRunning, obtainHolderInfo, onAnimationFinished, onAnimationStarted, recordPostLayoutInformation, recordPreLayoutInformation, setAddDuration, setChangeDuration, setMoveDuration, setRemoveDuration
public void runPendingAnimations()
RecyclerView.ItemAnimator
animateAppearance()
,
animateChange()
animatePersistence()
, and
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.runPendingAnimations
in class RecyclerView.ItemAnimator
public boolean animateRemove(RecyclerView.ViewHolder holder)
SimpleItemAnimator
#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 RecyclerView.ItemAnimator.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 RecyclerView.ItemAnimator.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.
animateRemove
in class SimpleItemAnimator
holder
- The item that is being removed.RecyclerView.ItemAnimator.runPendingAnimations()
is requested,
false otherwise.public boolean animateAdd(RecyclerView.ViewHolder holder)
SimpleItemAnimator
#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 RecyclerView.ItemAnimator.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 RecyclerView.ItemAnimator.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.
animateAdd
in class SimpleItemAnimator
holder
- The item that is being added.RecyclerView.ItemAnimator.runPendingAnimations()
is requested,
false otherwise.public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY)
SimpleItemAnimator
#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 RecyclerView.ItemAnimator.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 RecyclerView.ItemAnimator.runPendingAnimations()
.animateMove
in class SimpleItemAnimator
holder
- The item that is being moved.RecyclerView.ItemAnimator.runPendingAnimations()
is requested,
false otherwise.public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY)
SimpleItemAnimator
RecyclerView.Adapter.notifyItemChanged(int)
or
RecyclerView.Adapter.notifyItemRangeChanged(int, int)
.
Implementers can choose whether and how to animate changes, but must always call
#dispatchChangeFinished(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(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 RecyclerView.ItemAnimator.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 RecyclerView.ItemAnimator.runPendingAnimations()
.
animateChange
in class SimpleItemAnimator
oldHolder
- The original item that changed.newHolder
- The new item that was created with the changed content. Might be nullfromX
- Left of the old view holderfromY
- Top of the old view holdertoX
- Left of the new view holdertoY
- Top of the new view holderRecyclerView.ItemAnimator.runPendingAnimations()
is requested,
false otherwise.public void endAnimation(RecyclerView.ViewHolder item)
RecyclerView.ItemAnimator
#dispatchAnimationFinished(ViewHolder)
should be called for each finished
animation since the animations are effectively done when this method is called.endAnimation
in class RecyclerView.ItemAnimator
item
- The item for which an animation should be stopped.public boolean isRunning()
RecyclerView.ItemAnimator
isRunning
in class RecyclerView.ItemAnimator
public void endAnimations()
RecyclerView.ItemAnimator
#dispatchAnimationFinished(ViewHolder)
should be called for each finished
animation since the animations are effectively done when this method is called.endAnimations
in class RecyclerView.ItemAnimator
public boolean canReuseUpdatedViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull List<Object> payloads)
Note that this method will only be called if the RecyclerView.ViewHolder
still has the same
type (RecyclerView.Adapter.getItemViewType(int)
). Otherwise, ItemAnimator will always receive
both RecyclerView.ViewHolder
s in the
#animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)
method.
If the payload list is not empty, DefaultItemAnimator returns true
.
When this is the case:
#animateChange(ViewHolder, ViewHolder, int, int, int, int)
, both
ViewHolder arguments will be the same instance.
#animateChange(ViewHolder, ViewHolder, int, int, int, int)
,
then DefaultItemAnimator will call #animateMove(ViewHolder, int, int, int, int)
and
run a move animation instead.
canReuseUpdatedViewHolder
in class RecyclerView.ItemAnimator
viewHolder
- The ViewHolder which represents the changed item's old content.payloads
- A non-null list of merged payloads that were sent with change
notifications. Can be empty if the adapter is invalidated via
RecyclerView.Adapter.notifyDataSetChanged()
. The same list of
payloads will be passed into
RecyclerView.Adapter#onBindViewHolder(ViewHolder, int, List)
method if this method returns true
.RecyclerView.canReuseUpdatedViewHolder(ViewHolder)
.RecyclerView.canReuseUpdatedViewHolder(ViewHolder)