1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14 package android.support.v7.widget; 15 16 import java.util.ArrayList; 17 import java.util.concurrent.CountDownLatch; 18 import java.util.concurrent.TimeUnit; 19 20 public class LoggingItemAnimator extends DefaultItemAnimator { 21 22 final ArrayList<RecyclerView.ViewHolder> mAddVHs = new ArrayList<RecyclerView.ViewHolder>(); 23 24 final ArrayList<RecyclerView.ViewHolder> mRemoveVHs = new ArrayList<RecyclerView.ViewHolder>(); 25 26 final ArrayList<RecyclerView.ViewHolder> mMoveVHs = new ArrayList<RecyclerView.ViewHolder>(); 27 28 final ArrayList<RecyclerView.ViewHolder> mChangeOldVHs = new ArrayList<RecyclerView.ViewHolder>(); 29 30 final ArrayList<RecyclerView.ViewHolder> mChangeNewVHs = new ArrayList<RecyclerView.ViewHolder>(); 31 32 CountDownLatch mWaitForPendingAnimations; 33 34 @Override runPendingAnimations()35 public void runPendingAnimations() { 36 if (mWaitForPendingAnimations != null) { 37 mWaitForPendingAnimations.countDown(); 38 } 39 super.runPendingAnimations(); 40 } 41 expectRunPendingAnimationsCall(int count)42 public void expectRunPendingAnimationsCall(int count) { 43 mWaitForPendingAnimations = new CountDownLatch(count); 44 } 45 waitForPendingAnimationsCall(int seconds)46 public void waitForPendingAnimationsCall(int seconds) throws InterruptedException { 47 mWaitForPendingAnimations.await(seconds, TimeUnit.SECONDS); 48 } 49 50 @Override animateAdd(RecyclerView.ViewHolder holder)51 public boolean animateAdd(RecyclerView.ViewHolder holder) { 52 mAddVHs.add(holder); 53 return super.animateAdd(holder); 54 } 55 56 @Override animateRemove(RecyclerView.ViewHolder holder)57 public boolean animateRemove(RecyclerView.ViewHolder holder) { 58 mRemoveVHs.add(holder); 59 return super.animateRemove(holder); 60 } 61 62 @Override animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY)63 public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, 64 int toX, int toY) { 65 mMoveVHs.add(holder); 66 return super.animateMove(holder, fromX, fromY, toX, toY); 67 } 68 69 @Override animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY)70 public boolean animateChange(RecyclerView.ViewHolder oldHolder, 71 RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) { 72 if (oldHolder != null) { 73 mChangeOldVHs.add(oldHolder); 74 } 75 if (newHolder != null) { 76 mChangeNewVHs.add(newHolder); 77 } 78 return super.animateChange(oldHolder, newHolder, fromX, fromY, toX, toY); 79 } 80 reset()81 public void reset() { 82 mAddVHs.clear(); 83 mRemoveVHs.clear(); 84 mMoveVHs.clear(); 85 mChangeOldVHs.clear(); 86 mChangeNewVHs.clear(); 87 } 88 }