-
-
Save Miha-x64/ad6dbcc8d14843065d96355402fb5ab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aquadc.commonandroid.lists; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v4.view.ViewPropertyAnimatorCompat; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import com.mikepenz.itemanimators.DefaultAnimator; | |
/** | |
* ItemAnimator implementation which animates items so it looks like they're falling into list. | |
* Forked from <a href="https://gist.github.com/enq3/2dae8e769e9606349b22fdbdf65c41a1">enq3's animator</a>. | |
* Extends DefaultAnimator written by Mike Penz and available at | |
* <a href="https://github.com/mikepenz/ItemAnimators/blob/develop/library/src/main/java/com/mikepenz/itemanimators/DefaultAnimator.java}">his GitHub repo</a>. | |
* | |
* Sample usage: {@code recycler.setItemAnimator(new ItemAnimator().withPageSize(10).withInterpolator(new DecelerateInterpolator(.7f)).withAddDuration(300))} | |
*/ | |
public class ItemAnimator extends DefaultAnimator<ItemAnimator> { | |
/** | |
* I don't know why on earth you may need to disable animation, I'll just leave it here. | |
*/ | |
private boolean useAnimation = true; | |
private int durationPerOffset = 50; | |
private int delayPerOffset = 25; | |
private int pageSize = Integer.MAX_VALUE; | |
public boolean isUseAnimation() { return useAnimation; } | |
public int getDurationPerOffset() { return durationPerOffset; } | |
public int getDelayPerOffset() { return delayPerOffset; } | |
public int getPageSize() { return pageSize; } | |
public ItemAnimator withAnimation(boolean useAnimation) { | |
this.useAnimation = useAnimation; | |
return this; | |
} | |
public ItemAnimator withAnimationParametersPerOffset(int durationPerOffset, int delayPerOffset) { | |
this.durationPerOffset = durationPerOffset; | |
this.delayPerOffset = delayPerOffset; | |
return this; | |
} | |
/** | |
* When you're loading items in pages, next page will be animated slower without this correction. | |
* To turn off this correction, set to {@link Integer.MAX_VALUE} | |
*/ | |
public ItemAnimator withPageSize(int pageSize) { | |
this.pageSize = pageSize; | |
return this; | |
} | |
/** | |
* Just proxies your value to {@link this#setAddDuration} and returns itself. | |
*/ | |
public ItemAnimator withAddDuration(int addDuration) { | |
setAddDuration(addDuration); | |
return this; | |
} | |
@Override public void addAnimationPrepare(RecyclerView.ViewHolder holder) { | |
if (useAnimation) { | |
ViewCompat.setTranslationY(holder.itemView, holder.itemView.getHeight() * 0.4f); | |
ViewCompat.setAlpha(holder.itemView, 0.0f); | |
} | |
} | |
@Override public ViewPropertyAnimatorCompat addAnimation(final RecyclerView.ViewHolder holder) { | |
holder.itemView.setLayerType(View.LAYER_TYPE_HARDWARE, null); | |
int off = holder.getAdapterPosition() < 0 ? holder.getLayoutPosition() : holder.getAdapterPosition(); | |
while (off >= pageSize) off -= pageSize; | |
return ViewCompat.animate(holder.itemView) | |
.alpha(1) | |
.translationY(0) | |
.setDuration(useAnimation ? getAddDuration() + durationPerOffset * off : 0) | |
.setInterpolator(getInterpolator()) | |
.setStartDelay(useAnimation ? off * delayPerOffset : 0); | |
} | |
@Override public void addAnimationCleanup(RecyclerView.ViewHolder holder) { | |
ViewCompat.setAlpha(holder.itemView, 1); | |
ViewCompat.setTranslationY(holder.itemView, 0); | |
} | |
@Override public ViewPropertyAnimatorCompat removeAnimation(final RecyclerView.ViewHolder holder) { | |
holder.itemView.setLayerType(View.LAYER_TYPE_HARDWARE, null); | |
return ViewCompat.animate(holder.itemView) | |
.alpha(0) | |
.setDuration(useAnimation ? getRemoveDuration() : 0) | |
.setInterpolator(getInterpolator()) | |
.setStartDelay(0); | |
} | |
@Override public void removeAnimationCleanup(RecyclerView.ViewHolder holder) { | |
ViewCompat.setAlpha(holder.itemView, 1); | |
} | |
@Override public ViewPropertyAnimatorCompat changeOldAnimation(RecyclerView.ViewHolder holder, ChangeInfo changeInfo) { | |
return ViewCompat.animate(holder.itemView) | |
.setDuration(getChangeDuration()) | |
.alpha(0) | |
.translationX(changeInfo.toX - changeInfo.fromX) | |
.translationY(changeInfo.toY - changeInfo.fromY) | |
.setInterpolator(getInterpolator()); | |
} | |
@Override public ViewPropertyAnimatorCompat changeNewAnimation(RecyclerView.ViewHolder holder) { | |
return ViewCompat.animate(holder.itemView) | |
.translationX(0) | |
.translationY(0) | |
.setDuration(getChangeDuration()) | |
.alpha(1) | |
.setInterpolator(getInterpolator()); | |
} | |
@Override public void changeAnimationCleanup(RecyclerView.ViewHolder holder) { | |
ViewCompat.setAlpha(holder.itemView, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment