Permalink
Browse files

Add collapse animation

  • Loading branch information...
david-szabo97 committed Dec 11, 2017
1 parent e63660f commit 6f651434025b8102ca313d83467fa2c4891da30f
@@ -0,0 +1,50 @@
package com.messedcode.swipecollapseanimationexample;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
public class CustomItemAnimator extends DefaultItemAnimator {
public static final Interpolator COLLAPSE_INTERPOLATOR = new AccelerateInterpolator(3f);
public static final int COLLAPSE_ANIM_DURATION = 600;
@Override
public boolean canReuseUpdatedViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) {
return true;
}
@Override
public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder, @NonNull final RecyclerView.ViewHolder newHolder, @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) {
final View itemView = newHolder.itemView;
AnimatorSet set = new AnimatorSet();
LayoutParamHeightAnimator animHeight = LayoutParamHeightAnimator.collapse(itemView);
animHeight.setDuration(COLLAPSE_ANIM_DURATION).setInterpolator(COLLAPSE_INTERPOLATOR);
set.play(animHeight);
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
dispatchChangeStarting(newHolder, false);
}
@Override
public void onAnimationEnd(Animator animator) {
dispatchChangeFinished(newHolder, false);
}
});
set.start();
return false;
}
}
@@ -46,9 +46,30 @@ protected void onCreate(Bundle savedInstanceState) {
.leftIcon(convertDrawableToBitmap(deleteIconWhite))
.rightBackgroundColor(green)
.rightIcon(convertDrawableToBitmap(checkIconWhite))
.onSwipeListener(new CustomItemTouchHelperCallback.OnSwipeListener() {
@Override
public void onSwipeRight(RecyclerView.ViewHolder vh) {
int position = vh.getAdapterPosition();
Item item = adapter.getItemAt(position);
item.status = Item.Status.CHECKED;
adapter.notifyItemChanged(position);
}
@Override
public void onSwipeLeft(RecyclerView.ViewHolder vh) {
int position = vh.getAdapterPosition();
Item item = adapter.getItemAt(position);
item.status = Item.Status.DELETED;
adapter.notifyItemChanged(position);
}
})
.build();
ItemTouchHelper helper = new ItemTouchHelper(helperCb);
helper.attachToRecyclerView(recyclerView);
// Add animation
CustomItemAnimator animator = new CustomItemAnimator();
recyclerView.setItemAnimator(animator);
}
private float dpToPx(float dp) {

0 comments on commit 6f65143

Please sign in to comment.