Skip to content

Instantly share code, notes, and snippets.

@BenDLH
Created January 16, 2017 13:02
Show Gist options
  • Save BenDLH/8621a8a0186b3612663c2ecd4adbe60b to your computer and use it in GitHub Desktop.
Save BenDLH/8621a8a0186b3612663c2ecd4adbe60b to your computer and use it in GitHub Desktop.
SlideItemAnimator
package dk.shape.goboat.animation.animators;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import dk.shape.goboat.animation.utils.AnimUtils;
import dk.shape.goboat.animation.utils.SimpleAnimatorListener;
//
// SlideItemAnimator
// GoBoat
//
// Created by Ben De La Haye on 13/01/2017.
// Copyright (c) 2017 SHAPE A/S. All rights reserved.
//
public class SlideItemAnimator extends DefaultItemAnimator {
@Override
public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
return false;
}
@Override
public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) {
ObjectAnimator slideAnimator = ObjectAnimator.ofFloat(viewHolder.itemView, "translationX", -viewHolder.itemView.getWidth());
ObjectAnimator fadeAnimator = ObjectAnimator.ofInt(viewHolder.itemView, "alpha", 0);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(slideAnimator, fadeAnimator);
animatorSet.setDuration(200);
fadeAnimator.setInterpolator(AnimUtils.getExitInterpolator());
animatorSet.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
dispatchAnimationFinished(viewHolder);
dispatchRemoveFinished(viewHolder);
}
});
animatorSet.start();
dispatchRemoveStarting(viewHolder);
return true;
}
@Override
public boolean animateAppearance(@NonNull RecyclerView.ViewHolder viewHolder, @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
ObjectAnimator fadeAnimator = ObjectAnimator.ofInt(viewHolder.itemView, "alpha", 0, 1);
fadeAnimator.setDuration(200);
fadeAnimator.setInterpolator(AnimUtils.getEnterInterpolator());
fadeAnimator.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
dispatchAnimationFinished(viewHolder);
dispatchAddFinished(viewHolder);
}
});
fadeAnimator.start();
dispatchAddStarting(viewHolder);
return true;
}
@Override
public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY) {
final View view = holder.itemView;
fromX += ViewCompat.getTranslationX(view);
fromY += ViewCompat.getTranslationY(view);
int deltaX = toX - fromX;
int deltaY = toY - fromY;
if (deltaX == 0 && deltaY == 0) {
dispatchMoveFinished(holder);
return false;
}
if (deltaX != 0) {
ViewCompat.setTranslationX(view, -deltaX);
}
if (deltaY != 0) {
ViewCompat.setTranslationY(view, -deltaY);
}
ObjectAnimator horizontalSlideAnimator = ObjectAnimator.ofFloat(view, "translationX", 0);
ObjectAnimator verticalSlideAnimator = ObjectAnimator.ofFloat(view, "translationY", 0);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(horizontalSlideAnimator, verticalSlideAnimator);
animatorSet.setStartDelay(200);
animatorSet.setDuration(300);
animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
animatorSet.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
dispatchMoveStarting(holder);
}
@Override
public void onAnimationEnd(Animator animation) {
dispatchAnimationFinished(holder);
dispatchMoveFinished(holder);
}
});
animatorSet.start();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment