Allow swiped view recycling ASAP

This CL fixes a bug in ItemTouchHelper where it would assume that the
swiped view will be removed by the RecyclerView even if its animation
count does not reach 0.

This was a bad assumption and broke w/ the RecyclerView cleanups.

This CL changes ItemTouchHelper to allow view's recycling instantly
but do the cleanup when the view is detached (e.g. all other
animations etc. allows it to be removed).

This CL also fixes a bug in ItemAnimatorV2 test where it would call dispatch
end twice for change animations.

Bug: 25037922
Change-Id: I8c264ef1c7a89cb03f967796a0e14739f9e1945b
diff --git a/v7/recyclerview/src/android/support/v7/widget/helper/ItemTouchHelper.java b/v7/recyclerview/src/android/support/v7/widget/helper/ItemTouchHelper.java
index 0c22d1c..72d3e8c 100644
--- a/v7/recyclerview/src/android/support/v7/widget/helper/ItemTouchHelper.java
+++ b/v7/recyclerview/src/android/support/v7/widget/helper/ItemTouchHelper.java
@@ -873,7 +873,6 @@
                     anim.cancel();
                 }
                 mRecoverAnimations.remove(i);
-                anim.mViewHolder.setIsRecyclable(true);
                 return anim.mAnimationType;
             }
         }
@@ -1902,7 +1901,6 @@
                 final RecoverAnimation anim = recoverAnimationList.get(i);
                 if (anim.mEnded && !anim.mIsPendingCleanup) {
                     recoverAnimationList.remove(i);
-                    anim.mViewHolder.setIsRecyclable(true);
                 } else if (!anim.mEnded) {
                     hasRunningAnimation = true;
                 }
@@ -2314,6 +2312,9 @@
 
         @Override
         public void onAnimationEnd(ValueAnimatorCompat animation) {
+            if (!mEnded) {
+                mViewHolder.setIsRecyclable(true);
+            }
             mEnded = true;
         }
 
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/ItemAnimatorV2ApiTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/ItemAnimatorV2ApiTest.java
index 3ef0b3f..5733e720 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/ItemAnimatorV2ApiTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/ItemAnimatorV2ApiTest.java
@@ -506,7 +506,7 @@
                 dispatchAnimationFinished(oldHolder);
                 assertSame(preLayoutInfoMap.get(oldHolder), preInfo);
             }
-            if (newHolder != null) {
+            if (newHolder != null && oldHolder != newHolder) {
                 dispatchAnimationFinished(newHolder);
                 assertSame(postLayoutInfoMap.get(newHolder), postInfo);
             }