onFailedToRecycleView
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state. Upon receiving this callback, Adapter can clear the animation(s) that effect the View's transient state and return true
so that the View can be recycled. Keep in mind that the View in question is already removed from the RecyclerView.
In some cases, it is acceptable to recycle a View although it has transient state. Most of the time, this is a case where the transient state will be cleared in onBindViewHolder call when View is rebound to a new position. For this reason, RecyclerView leaves the decision to the Adapter and uses the return value of this method to decide whether the View should be recycled or not.
Note that when all animations are created by RecyclerView.ItemAnimator, you should never receive this callback because RecyclerView keeps those Views as children until their animations are complete. This callback is useful when children of the item views create animations which may not be easy to implement using an ItemAnimator.
You should never fix this issue by calling holder.itemView.setHasTransientState(false);
unless you've previously called holder.itemView.setHasTransientState(true);
. Each View.setHasTransientState(true)
call must be matched by a View.setHasTransientState(false)
call, otherwise, the state of the View may become inconsistent. You should always prefer to end or cancel animations that are triggering the transient state instead of handling it manually.
Return
True if the View should be recycled, false otherwise. Note that if this method returns true
, RecyclerView will ignore the transient state of the View and recycle it regardless. If this method returns false
, RecyclerView will check the View's transient state again before giving a final decision. Default implementation returns false.
Parameters
The ViewHolder containing the View that could not be recycled due to its transient state.