Skip to content

Commit

Permalink
Improved item animator logic.
Browse files Browse the repository at this point in the history
Thanks @yigit!
  • Loading branch information
nickbutcher committed May 10, 2016
1 parent 7395e1a commit 84b256a
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public boolean animateAdd(RecyclerView.ViewHolder holder) {
public void runPendingAnimations() {
super.runPendingAnimations();
if (!pendingAdds.isEmpty()) {
for (final RecyclerView.ViewHolder holder : pendingAdds) {
for (int i = pendingAdds.size() - 1; i >= 0; i--) {
final RecyclerView.ViewHolder holder = pendingAdds.get(i);
holder.itemView.animate()
.alpha(1f)
.translationX(0f)
Expand All @@ -89,17 +90,17 @@ public void onAnimationStart(Animator animation) {
public void onAnimationEnd(Animator animation) {
animation.getListeners().remove(this);
dispatchAddFinished(holder);
pendingAdds.remove(holder);
dispatchFinishedWhenDone();
}

@Override
public void onAnimationCancel(Animator animation) {
endViewAnimation(holder.itemView);
clearAnimatedValues(holder.itemView);
}
})
.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(
holder.itemView.getContext()));
pendingAdds.remove(i);
}
}
}
Expand All @@ -108,7 +109,8 @@ public void onAnimationCancel(Animator animation) {
public void endAnimation(RecyclerView.ViewHolder holder) {
holder.itemView.animate().cancel();
if (pendingAdds.remove(holder)) {
endViewAnimation(holder.itemView);
dispatchAddFinished(holder);
clearAnimatedValues(holder.itemView);
}
super.endAnimation(holder);
}
Expand All @@ -117,7 +119,7 @@ public void endAnimation(RecyclerView.ViewHolder holder) {
public void endAnimations() {
for (int i = pendingAdds.size() - 1; i >= 0; i--) {
final RecyclerView.ViewHolder holder = pendingAdds.get(i);
endViewAnimation(holder.itemView);
clearAnimatedValues(holder.itemView);
dispatchAddFinished(holder);
pendingAdds.remove(i);
}
Expand All @@ -135,7 +137,7 @@ private void dispatchFinishedWhenDone() {
}
}

private void endViewAnimation(final View view) {
private void clearAnimatedValues(final View view) {
view.setAlpha(1f);
view.setTranslationX(0f);
view.setTranslationY(0f);
Expand Down

0 comments on commit 84b256a

Please sign in to comment.