Skip to content

Instantly share code, notes, and snippets.

@frogermcs frogermcs/animateAdd.java
Last active Oct 7, 2016

Embed
What would you like to do?
InstaMaterial source files (RecyclerView done right)
@Override
public boolean animateAdd(RecyclerView.ViewHolder viewHolder) {
if (viewHolder.getItemViewType() == FeedAdapter.VIEW_TYPE_DEFAULT) {
if (viewHolder.getLayoutPosition() > lastAddAnimatedItem) {
lastAddAnimatedItem++;
runEnterAnimation((FeedAdapter.CellFeedViewHolder) viewHolder);
return false;
}
}
dispatchAddFinished(viewHolder);
return false;
}
@Override
public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder,
@NonNull RecyclerView.ViewHolder newHolder,
@NonNull ItemHolderInfo preInfo,
@NonNull ItemHolderInfo postInfo) {
cancelCurrentAnimationIfExists(newHolder);
if (preInfo instanceof FeedItemHolderInfo) {
FeedItemHolderInfo feedItemHolderInfo = (FeedItemHolderInfo) preInfo;
FeedAdapter.CellFeedViewHolder holder = (FeedAdapter.CellFeedViewHolder) newHolder;
animateHeartButton(holder);
updateLikesCounter(holder, holder.getFeedItem().likesCount);
if (FeedAdapter.ACTION_LIKE_IMAGE_CLICKED.equals(feedItemHolderInfo.updateAction)) {
animatePhotoLike(holder);
}
}
return false;
}
@Override
public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
return true;
}
@Override
public void endAnimation(RecyclerView.ViewHolder item) {
super.endAnimation(item);
cancelCurrentAnimationIfExists(item);
}
@Override
public void endAnimations() {
super.endAnimations();
for (AnimatorSet animatorSet : likeAnimationsMap.values()) {
animatorSet.cancel();
}
}
holder.btnComments.setTag(position);
holder.btnMore.setTag(position);
private static final DecelerateInterpolator DECCELERATE_INTERPOLATOR = new DecelerateInterpolator();
private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
private static final OvershootInterpolator OVERSHOOT_INTERPOLATOR = new OvershootInterpolator(4);
private static final int ANIMATED_ITEMS_COUNT = 2;
private final Map<RecyclerView.ViewHolder, AnimatorSet> likeAnimations = new HashMap<>();
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
//...
bindLoadingFeedItem(holder);
}
private void bindDefaultFeedItem(int position, CellFeedViewHolder holder) {
//...
updateLikesCounter(holder, false);
updateHeartButton(holder, false);
holder.btnComments.setTag(position);
holder.btnMore.setTag(position);
holder.ivFeedCenter.setTag(holder);
holder.btnLike.setTag(holder);
if (likeAnimations.containsKey(holder)) {
likeAnimations.get(holder).cancel();
}
resetLikeAnimationState(holder);
}
@Override
public void onClick(View view) {
//...
if (viewId == R.id.btnComments) {
//...
} else if (viewId == R.id.btnMore) {
if (onFeedItemClickListener != null) {
onFeedItemClickListener.onMoreClick(view, (Integer) view.getTag());
}
}
//...
}
@NonNull
@Override
public ItemHolderInfo recordPreLayoutInformation(@NonNull RecyclerView.State state,
@NonNull RecyclerView.ViewHolder viewHolder,
int changeFlags, @NonNull List<Object> payloads) {
if (changeFlags == FLAG_CHANGED) {
for (Object payload : payloads) {
if (payload instanceof String) {
return new FeedItemHolderInfo((String) payload);
}
}
}
return super.recordPreLayoutInformation(state, viewHolder, changeFlags, payloads);
}
private void runEnterAnimation(View view, int position) {
if (!animateItems || position >= ANIMATED_ITEMS_COUNT - 1) {
return;
}
if (position > lastAnimatedPosition) {
lastAnimatedPosition = position;
view.setTranslationY(Utils.getScreenHeight(context));
view.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator(3.f))
.setDuration(700)
.start();
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
runEnterAnimation(viewHolder.itemView, position);
//...
}
private boolean showLoadingView = false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.