Skip to content

Commit

Permalink
Improved SlideInItemAnimator & use it liberally.
Browse files Browse the repository at this point in the history
- Handle endAnimation(s) calls
– Support different slide directions.
– Use animator on Home, Search & PlayerSheet.
  • Loading branch information
nickbutcher committed May 9, 2016
1 parent d8ccc6f commit 487eeff
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/io/plaidapp/ui/HomeGridItemAnimator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@

import java.util.List;

import io.plaidapp.ui.recyclerview.SlideInItemAnimator;
import io.plaidapp.util.AnimUtils;
import io.plaidapp.util.ViewUtils;

/**
* An extension to {@link DefaultItemAnimator} for running animations specific to our home grid.
*/
public class HomeGridItemAnimator extends DefaultItemAnimator {
public class HomeGridItemAnimator extends SlideInItemAnimator {

public static final int ANIMATE_ADD_POCKET = 7;

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/io/plaidapp/ui/PlayerSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import io.plaidapp.data.api.dribbble.model.Shot;
import io.plaidapp.data.api.dribbble.model.User;
import io.plaidapp.ui.recyclerview.InfiniteScrollListener;
import io.plaidapp.ui.recyclerview.SlideInItemAnimator;
import io.plaidapp.ui.widget.BottomSheet;
import io.plaidapp.util.DribbbleUtils;
import io.plaidapp.util.glide.CircleTransform;
Expand Down Expand Up @@ -185,6 +186,7 @@ public void onSheetPositionChanged(int sheetTop, boolean interacted) {

layoutManager = new LinearLayoutManager(this);
playerList.setLayoutManager(layoutManager);
playerList.setItemAnimator(new SlideInItemAnimator());
adapter = new PlayerAdapter(this);
dataManager.registerCallback(adapter);
playerList.setAdapter(adapter);
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/io/plaidapp/ui/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import io.plaidapp.data.SearchDataManager;
import io.plaidapp.data.pocket.PocketUtils;
import io.plaidapp.ui.recyclerview.InfiniteScrollListener;
import io.plaidapp.ui.recyclerview.SlideInItemAnimator;
import io.plaidapp.ui.widget.BaselineGridTextView;
import io.plaidapp.util.AnimUtils;
import io.plaidapp.util.ImeUtils;
Expand Down Expand Up @@ -150,6 +151,7 @@ public void onDataLoaded(List<? extends PlaidItem> data) {
};
adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this));
results.setAdapter(adapter);
results.setItemAnimator(new SlideInItemAnimator());
GridLayoutManager layoutManager = new GridLayoutManager(this, columns);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.animation.AnimatorListenerAdapter;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,15 +34,33 @@
public class SlideInItemAnimator extends DefaultItemAnimator {

private final List<RecyclerView.ViewHolder> pendingAdds = new ArrayList<>();
private final int slideFromEdge;

public SlideInItemAnimator() {
this(Gravity.BOTTOM, -1); // undefined layout dir; bottom isn't relative
}

public SlideInItemAnimator(int slideFromEdge, int layoutDirection) {
this.slideFromEdge = Gravity.getAbsoluteGravity(slideFromEdge, layoutDirection);
setAddDuration(160L);
}

@Override
public boolean animateAdd(RecyclerView.ViewHolder holder) {
holder.itemView.setAlpha(0f);
holder.itemView.setTranslationY(holder.itemView.getHeight() / 3);
switch (slideFromEdge) {
case Gravity.LEFT:
holder.itemView.setTranslationX(-holder.itemView.getWidth() / 3);
break;
case Gravity.TOP:
holder.itemView.setTranslationY(-holder.itemView.getHeight() / 3);
break;
case Gravity.RIGHT:
holder.itemView.setTranslationX(holder.itemView.getWidth() / 3);
break;
default: // Gravity.BOTTOM
holder.itemView.setTranslationY(holder.itemView.getHeight() / 3);
}
pendingAdds.add(holder);
return true;
}
Expand All @@ -52,6 +72,7 @@ public void runPendingAnimations() {
for (final RecyclerView.ViewHolder holder : pendingAdds) {
holder.itemView.animate()
.alpha(1f)
.translationX(0f)
.translationY(0f)
.setDuration(getAddDuration())
.setListener(new AnimatorListenerAdapter() {
Expand All @@ -65,24 +86,55 @@ public void onAnimationEnd(Animator animation) {
animation.getListeners().remove(this);
dispatchAddFinished(holder);
pendingAdds.remove(holder);
if (!isRunning()) dispatchAnimationsFinished();
dispatchFinishedWhenDone();
}

@Override
public void onAnimationCancel(Animator animation) {
holder.itemView.setAlpha(1f);
holder.itemView.setTranslationY(0f);
endViewAnimation(holder.itemView);
}
})
.setInterpolator(
AnimUtils.getLinearOutSlowInInterpolator(holder.itemView.getContext()));
.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(
holder.itemView.getContext()));
}
}
}

@Override
public void endAnimation(RecyclerView.ViewHolder holder) {
holder.itemView.animate().cancel();
if (pendingAdds.remove(holder)) {
endViewAnimation(holder.itemView);
}
super.endAnimation(holder);
}

@Override
public void endAnimations() {
for (int i = pendingAdds.size() - 1; i >= 0; i--) {
final RecyclerView.ViewHolder holder = pendingAdds.get(i);
endViewAnimation(holder.itemView);
dispatchAddFinished(holder);
pendingAdds.remove(i);
}
super.endAnimations();
}

@Override
public boolean isRunning() {
return super.isRunning() || !pendingAdds.isEmpty();
return !pendingAdds.isEmpty() || super.isRunning();
}

private void dispatchFinishedWhenDone() {
if (!isRunning()) {
dispatchAnimationsFinished();
}
}

private void endViewAnimation(final View view) {
view.setAlpha(1f);
view.setTranslationX(0f);
view.setTranslationY(0f);
}

}

0 comments on commit 487eeff

Please sign in to comment.