Skip to content

Commit

Permalink
add animation
Browse files Browse the repository at this point in the history
  • Loading branch information
donniesky committed May 2, 2017
1 parent c086dae commit 89c6fac
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 4 deletions.
119 changes: 115 additions & 4 deletions library/src/main/java/me/donnie/adapter/MultiItemAdapter.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package me.donnie.adapter;

import android.animation.Animator;
import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import me.donnie.adapter.animation.AlphaInAnimation;
import me.donnie.adapter.animation.BaseAnimation;
import me.donnie.adapter.animation.ScaleInAnimation;
import me.donnie.adapter.animation.SlideInBottomAnimation;
import me.donnie.adapter.animation.SlideInLeftAnimation;
import me.donnie.adapter.delegate.ItemViewDelegate;
import me.donnie.adapter.delegate.ItemViewDelegateManager;

Expand All @@ -36,8 +47,6 @@ public abstract class MultiItemAdapter<T, K extends BaseViewHolder> extends Recy

public static final int SLIDEIN_LEFT = 0x00000004;

public static final int SLIDEIN_RIGHT = 0x00000005;

public static final int HEADER_VIEW = 0x00000111;
public static final int LOADING_VIEW = 0x00000222;
public static final int FOOTER_VIEW = 0x00000333;
Expand All @@ -48,7 +57,17 @@ public abstract class MultiItemAdapter<T, K extends BaseViewHolder> extends Recy
private OnItemLongClickListener mOnItemLongClickListener;
private OnItemChildLongClickListener mOnItemChildLongClickListener;

@IntDef({ALPHAIN, SCALEIN, SLIDEIN_BOTTOM, SLIDEIN_LEFT, SLIDEIN_RIGHT})
private boolean mAnimEnable = false;

private int mLastPosition = -1;
private Interpolator mInterpolator = new LinearInterpolator();
private int mDuration = 300;

private BaseAnimation mCustomAnimation;
private BaseAnimation mSelectAnimation = new AlphaInAnimation();


@IntDef({ALPHAIN, SCALEIN, SLIDEIN_BOTTOM, SLIDEIN_LEFT})
@Retention(RetentionPolicy.SOURCE)
public @interface AnimationType {}

Expand All @@ -60,10 +79,27 @@ public abstract class MultiItemAdapter<T, K extends BaseViewHolder> extends Recy
protected LayoutInflater mInflater;
protected List<T> mDatas;

public void addNewData(@Nullable List<T> datas) {
this.mDatas = datas == null ? new ArrayList<T>() : datas;
mLastPosition = -1;
notifyDataSetChanged();
}

public void addData(@NonNull T data) {
mDatas.add(data);
notifyItemInserted(mDatas.size());
}

public void addData(List<T> datas) {
this.mDatas = datas == null ? new ArrayList<T>() : datas;
mDatas.addAll(datas);
notifyItemInserted(mDatas.size() - 1);
notifyItemRangeInserted(mDatas.size() - datas.size(), datas.size());
}

public void remove(@IntRange(from = 0) int position, @NonNull T data) {
mDatas.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDatas.size() - position);
}

public void clear() {
Expand All @@ -79,6 +115,40 @@ public T getItem(@IntRange(from = 0) int position) {
return mDatas.get(position);
}

public void enableAnim() {
this.mAnimEnable = true;
}

public void enableAnim(BaseAnimation animation) {
this.mAnimEnable = true;
this.mCustomAnimation = animation;
}

public void enableAnim(@AnimationType int type) {
this.mAnimEnable = true;
mCustomAnimation = null;
switch (type) {
case ALPHAIN:
mSelectAnimation = new AlphaInAnimation();
break;
case SCALEIN:
mSelectAnimation = new ScaleInAnimation();
break;
case SLIDEIN_BOTTOM:
mSelectAnimation = new SlideInBottomAnimation();
break;
case SLIDEIN_LEFT:
mSelectAnimation = new SlideInLeftAnimation();
break;
default:
break;
}
}

public void setDuration(int duration) {
mDuration = duration;
}

public MultiItemAdapter(List<T> datas) {
this.mDatas = datas == null ? new ArrayList<T>() : datas;
mItemViewDelegateManager = new ItemViewDelegateManager();
Expand Down Expand Up @@ -110,6 +180,47 @@ public int getItemCount() {
return mDatas.size();
}

@Override
public void onViewAttachedToWindow(K holder) {
super.onViewAttachedToWindow(holder);
int viewType = holder.getItemViewType();
if (viewType == EMPTY_VIEW || viewType == HEADER_VIEW || viewType == FOOTER_VIEW
|| viewType == LOADING_VIEW) {
setFullSpan(holder);
} else {

}
}

protected void setFullSpan(RecyclerView.ViewHolder holder) {
if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
params.setFullSpan(true);
}
}

private void addAnimation(RecyclerView.ViewHolder holder) {
if (mAnimEnable) {
if (holder.getLayoutPosition() > mLastPosition) {
BaseAnimation animation = null;
if (mCustomAnimation != null) {
animation = mCustomAnimation;
} else {
animation = mSelectAnimation;
}
for (Animator animator : animation.getAnimators(holder.itemView)) {
startAnim(animator, holder.getLayoutPosition());
}
mLastPosition = holder.getLayoutPosition();
}
}
}

protected void startAnim(Animator animator, int position) {
animator.setDuration(mDuration).start();
animator.setInterpolator(mInterpolator);
}

public void convert(K holder, T t) {
mItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.donnie.adapter.animation;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;

/**
* @author donnieSky
* @created_at 2017/5/2.
* @description
*/

public class AlphaInAnimation implements BaseAnimation {

private static final float DEFAULT_ALPHA_FROM = 0f;
private final float mFrom;

public AlphaInAnimation() {
this(DEFAULT_ALPHA_FROM);
}

public AlphaInAnimation(float from) {
mFrom = from;
}

@Override
public Animator[] getAnimators(View view) {
return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", mFrom, 1f)};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.donnie.adapter.animation;

import android.animation.Animator;
import android.view.View;

/**
* @author donnieSky
* @created_at 2017/5/2.
* @description
*/

public interface BaseAnimation {

Animator[] getAnimators(View view);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.donnie.adapter.animation;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;

/**
* @author donnieSky
* @created_at 2017/5/2.
* @description
*/

public class ScaleInAnimation implements BaseAnimation {

private static final float DEFAULT_SCALE_FROM = .5f;
private final float mFrom;

public ScaleInAnimation() {
this(DEFAULT_SCALE_FROM);
}

public ScaleInAnimation(float from) {
mFrom = from;
}


@Override
public Animator[] getAnimators(View view) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", mFrom, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", mFrom, 1f);
return new ObjectAnimator[] { scaleX, scaleY };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.donnie.adapter.animation;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;

/**
* @author donnieSky
* @created_at 2017/5/2.
* @description
*/

public class SlideInBottomAnimation implements BaseAnimation {

@Override
public Animator[] getAnimators(View view) {
return new Animator[]{
ObjectAnimator.ofFloat(view, "translationY", view.getMeasuredHeight(), 0)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.donnie.adapter.animation;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;

/**
* @author donnieSky
* @created_at 2017/5/2.
* @description
*/

public class SlideInLeftAnimation implements BaseAnimation {
@Override
public Animator[] getAnimators(View view) {
return new Animator[] {
ObjectAnimator.ofFloat(view, "translationX", -view.getRootView().getWidth(), 0)
};
}
}

0 comments on commit 89c6fac

Please sign in to comment.