酷炫的RecyclerView动画库 | recyclerview-animators

名称 recyclerview-animators
语言 Android
平台 GitHub
作者 wasabeef
链接 点此进入

今天给大家推荐一款酷炫的RecyclerView库,只要几句代码,就可以给RecyclerView的Item添加酷炫的动画。
效果图

  1. ItemAnimator
demo1.gif
demo2.gif
demo3.gif
  1. Adapters


    demo4.gif
demo5.gif

使用方法

  1. 添加依赖
dependencies {
  // jCenter
  compile 'jp.wasabeef:recyclerview-animators:2.2.7'
}
  1. 添加动画有两种方式,第一种是添加ItemAnimator动画,第二种请参见第3点
    (1)设置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)请勿直接使用notifyDataSetChanged()方法
建议采用一下方法取代notifyDataSetChanged(),如果直接使用notifyDataSetChanged()方法,则动画无法触发

notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)

(3)设置动画时长

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实现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();
  }
}
  1. 通过设置RecyclerView.Adapter添加动画
    (1)设置ItemAnimator
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(new AlphaInAnimationAdapter(adapter));

(2)设置动画时长

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setDuration(1000);
recyclerView.setAdapter(alphaAdapter);

(3)设置插值器

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setInterpolator(new OvershootInterpolator());
recyclerView.setAdapter(alphaAdapter);

(4)取消第一次滑动模式

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
scaleAdapter.setFirstOnly(false);
recyclerView.setAdapter(alphaAdapter);

(5)多重动画(可选)

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));

更多详细使用方法请参考其GitHub。

你可能感兴趣的:(酷炫的RecyclerView动画库 | recyclerview-animators)