名稱 | recyclerview-animators |
---|---|
語(yǔ)言 | Android |
平臺(tái) | GitHub |
作者 | wasabeef |
鏈接 | 點(diǎn)此進(jìn)入 |
今天給大家推薦一款酷炫的RecyclerView庫(kù),只要幾句代碼需忿,就可以給RecyclerView的Item添加酷炫的動(dòng)畫(huà)月而。
效果圖
- ItemAnimator
-
Adapters
使用方法
- 添加依賴
dependencies {
// jCenter
compile 'jp.wasabeef:recyclerview-animators:2.2.7'
}
- 添加動(dòng)畫(huà)有兩種方式包晰,第一種是添加ItemAnimator動(dòng)畫(huà)伐憾,第二種請(qǐng)參見(jiàn)第3點(diǎn)
(1)設(shè)置RecyclerView的ItemAnimator
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setItemAnimator(new SlideInLeftAnimator());
或者
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
SlideInUpAnimator animator = new SlideInUpAnimator(new OvershootInterpolator(1f)); //相比于第一種蒸矛,第二種為SlideInUpAnimator添加插值器
recyclerView.setItemAnimator(animator);
(2)請(qǐng)勿直接使用notifyDataSetChanged()方法
建議采用一下方法取代notifyDataSetChanged(),如果直接使用notifyDataSetChanged()方法,則動(dòng)畫(huà)無(wú)法觸發(fā)
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
(3)設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng)
recyclerView.getItemAnimator().setAddDuration(1000);
recyclerView.getItemAnimator().setRemoveDuration(1000);
recyclerView.getItemAnimator().setMoveDuration(1000);
recyclerView.getItemAnimator().setChangeDuration(1000);
(4)添加插值器
SlideInLeftAnimator animator = new SlideInLeftAnimator();
animator.setInterpolator(new OvershootInterpolator());
// or recyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));
recyclerView.setItemAnimator(animator);
(5)在ViewHolder實(shí)現(xiàn)AnimateViewHolder接口
static class MyViewHolder extends RecyclerView.ViewHolder implements AnimateViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
@Override
public void preAnimateRemoveImpl(RecyclerView.ViewHolder holder) {
}
@Override
public void animateRemoveImpl(RecyclerView.ViewHolder holder, ViewPropertyAnimatorListener listener) {
ViewCompat.animate(itemView)
.translationY(-itemView.getHeight() * 0.3f)
.alpha(0)
.setDuration(300)
.setListener(listener)
.start();
}
@Override
public void preAnimateAddImpl(RecyclerView.ViewHolder holder) {
ViewCompat.setTranslationY(itemView, -itemView.getHeight() * 0.3f);
ViewCompat.setAlpha(itemView, 0);
}
@Override
public void animateAddImpl(RecyclerView.ViewHolder holder, ViewPropertyAnimatorListener listener) {
ViewCompat.animate(itemView)
.translationY(0)
.alpha(1)
.setDuration(300)
.setListener(listener)
.start();
}
}
- 通過(guò)設(shè)置RecyclerView.Adapter添加動(dòng)畫(huà)
(1)設(shè)置ItemAnimator
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(new AlphaInAnimationAdapter(adapter));
(2)設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng)
MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setDuration(1000);
recyclerView.setAdapter(alphaAdapter);
(3)設(shè)置插值器
MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setInterpolator(new OvershootInterpolator());
recyclerView.setAdapter(alphaAdapter);
(4)取消第一次滑動(dòng)模式
MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
scaleAdapter.setFirstOnly(false);
recyclerView.setAdapter(alphaAdapter);
(5)多重動(dòng)畫(huà)(可選)
MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));
更多詳細(xì)使用方法請(qǐng)參考其GitHub纸肉。