private void animateAddImpl(final ViewHolder holder) { final View view = holder.itemView; final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); mAddAnimations.add(holder); float startX, startY; int position = (int) view.getTag(); if (position % 2 == 0) { startY = -100; startX = -width; } else { startY = 100; startX = width; } Log.d("test", String.valueOf(holder.getLayoutPosition())); ViewCompat.setTranslationX(view, startX); // 应该是相对偏移 ViewCompat.setTranslationY(view, startY); ViewCompat.setAlpha(view, 0); animation.setInterpolator(new OvershootInterpolator()); animation .alpha(1) .translationX(0) .translationY(0) .setDuration(getAddDuration()) .setListener( new VpaListenerAdapter() { @Override public void onAnimationStart(View view) { dispatchAddStarting(holder); } @Override public void onAnimationCancel(View view) { ViewCompat.setAlpha(view, 1); } @Override public void onAnimationEnd(View view) { animation.setListener(null); dispatchAddFinished(holder); mAddAnimations.remove(holder); dispatchFinishedWhenDone(); // 使用这个来复原 ViewCompat.setTranslationY(view, 0); } }) .start(); }
private void animateRemoveImpl(final ViewHolder holder) { final View view = holder.itemView; int position = (int) view.getTag(); final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); float endX, endY; if (position % 2 == 0) { endY = -100; endX = -width; } else { endY = 100; endX = width; } mRemoveAnimations.add(holder); animation.setInterpolator(new AnticipateOvershootInterpolator()); animation .setDuration(getRemoveDuration()) .alpha(0) .translationX(endX) .translationY(endY) .setListener( new VpaListenerAdapter() { @Override public void onAnimationStart(View view) { dispatchRemoveStarting(holder); } @Override public void onAnimationEnd(View view) { animation.setListener(null); // 重置为正常 ViewCompat.setAlpha(view, 1); ViewCompat.setTranslationX(view, 0); ViewCompat.setTranslationY(view, 0); dispatchRemoveFinished(holder); mRemoveAnimations.remove(holder); dispatchFinishedWhenDone(); } }) .start(); }
void start() { final View containerView = ((SwipeableItemViewHolder) mViewHolder).getSwipeableContainerView(); mInvSize = (1.0f / Math.max(1.0f, mHorizontal ? containerView.getWidth() : containerView.getHeight())); // setup animator mAnimator = ViewCompat.animate(containerView); mAnimator.setDuration(mDuration); mAnimator.translationX(mToX); mAnimator.translationY(mToY); if (mInterpolator != null) { mAnimator.setInterpolator(mInterpolator); } mAnimator.setListener(this); mAnimator.setUpdateListener(this); // start mActive.add(mViewHolder); mAnimator.start(); }