【整體刷新效果】
當第一次顯示RecyclerView
的Item或者往下滑動時的動畫效果如下:
【刷新指定Item】
刷新指定Item代碼:
urls.set(position, "http://img4.imgtn.bdimg.com/it/u=3209370120,2008812818&fm=26&gp=0.jpg");
recycleViewAdapter.notifyItemChanged(position);
【刪除效果】
刪除刷新代碼:
urls.remove(position);
recycleViewAdapter.notifyItemRemoved(position);
recycleViewAdapter.notifyItemRangeChanged(position, urls.size() - position);
【插入效果】
插入刷新代碼:
urls.add(position, "http://img4.imgtn.bdimg.com/it/u=3209370120,2008812818&fm=26&gp=0.jpg");
recycleViewAdapter.notifyItemInserted(position);
recycleViewAdapter.notifyItemRangeChanged(position, urls.size() - position);
【移動效果】
移動刷新代碼:
int fromPosition = position;
int toPosition = position + 1;
Collections.swap(urls,fromPosition,toPosition);
recycleViewAdapter.notifyItemMoved(fromPosition, toPosition);
recycleViewAdapter.notifyItemRangeChanged(position, toPosition - fromPosition + 1);
縮放動畫實現代碼如下:
AnimationAdapter.java
public abstract class AnimationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private RecyclerView.Adapter<RecyclerView.ViewHolder> mAdapter;
private int mDuration = 300;
private Interpolator mInterpolator = new LinearInterpolator();
private int mLastPosition = -1;
private boolean isFirstOnly = true;
public AnimationAdapter(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
mAdapter = adapter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return mAdapter.onCreateViewHolder(parent, viewType);
}
@Override
public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
super.registerAdapterDataObserver(observer);
mAdapter.registerAdapterDataObserver(observer);
}
@Override
public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
super.unregisterAdapterDataObserver(observer);
mAdapter.unregisterAdapterDataObserver(observer);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mAdapter.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
mAdapter.onDetachedFromRecyclerView(recyclerView);
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
mAdapter.onViewAttachedToWindow(holder);
}
@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
mAdapter.onViewDetachedFromWindow(holder);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mAdapter.onBindViewHolder(holder, position);
int adapterPosition = holder.getAdapterPosition();
if (!isFirstOnly || adapterPosition > mLastPosition) {
for (Animator anim : getAnimators(holder.itemView)) {
anim.setDuration(mDuration).start();
anim.setInterpolator(mInterpolator);
}
mLastPosition = adapterPosition;
} else {
holder.itemView.setScaleX(1);
holder.itemView.setScaleY(1);
}
}
@Override public void onViewRecycled(RecyclerView.ViewHolder holder) {
mAdapter.onViewRecycled(holder);
super.onViewRecycled(holder);
}
@Override
public int getItemCount() {
return mAdapter.getItemCount();
}
public void setDuration(int duration) {
mDuration = duration;
}
public void setInterpolator(Interpolator interpolator) {
mInterpolator = interpolator;
}
public void setStartPosition(int start) {
mLastPosition = start;
}
protected abstract Animator[] getAnimators(View view);
public void setFirstOnly(boolean firstOnly) {
isFirstOnly = firstOnly;
}
@Override
public int getItemViewType(int position) {
return mAdapter.getItemViewType(position);
}
public RecyclerView.Adapter<RecyclerView.ViewHolder> getWrappedAdapter() {
return mAdapter;
}
@Override
public long getItemId(int position) {
return mAdapter.getItemId(position);
}
}
ScaleInAnimationAdapter.java
public class ScaleInAnimationAdapter extends AnimationAdapter {
private static final float DEFAULT_SCALE_FROM = .5f;
private final float mFrom;
public ScaleInAnimationAdapter(RecyclerView.Adapter adapter) {
this(adapter, DEFAULT_SCALE_FROM);
}
public ScaleInAnimationAdapter(RecyclerView.Adapter adapter, float from) {
super(adapter);
mFrom = from;
}
@Override protected 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 };
}
}
使用縮放Adapter
ScaleInAnimationAdapter adapter = new ScaleInAnimationAdapter(recycleViewAdapter);
adapter.setDuration(3000);
adapter.setFirstOnly(false);
recyclerview.setAdapter(adapter);
[本章完...]