RecyclerView Animators

简介: 点击下载完整项目 一个Android动画库,可以轻松地添加itemanimator到RecyclerView项目。 特征 动画添加和删除ItemAnimator RecyclerView.Adapter中项目的外观动画 Demo ItemAnimator    Adapters   Setup Gradle



点击下载完整项目


一个Android动画库,可以轻松地添加itemanimator到RecyclerView项目。


特征


动画添加和删除ItemAnimator
RecyclerView.Adapter中项目的外观动画

Demo

ItemAnimator

  

Adapters

 



Setup

Gradle

dependencies {
  // jCenter
  compile 'jp.wasabeef:recyclerview-animators:2.2.5'
}

ItemAnimator

Step 1

Set RecyclerView ItemAnimator.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setItemAnimator(new SlideInLeftAnimator());
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));

Step 2

Please use the following
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)

If you want your animations to work, do not rely on calling notifyDataSetChanged(); as it is the RecyclerView's default behavior, animations are not triggered to start inside this method.

public void remove(int position) {
  mDataSet.remove(position);
  notifyItemRemoved(position);
}

public void add(String text, int position) {
  mDataSet.add(position, text);
  notifyItemInserted(position);
}

Advanced Step 3

You can change the durations.

recyclerView.getItemAnimator().setAddDuration(1000);
recyclerView.getItemAnimator().setRemoveDuration(1000);
recyclerView.getItemAnimator().setMoveDuration(1000);
recyclerView.getItemAnimator().setChangeDuration(1000);

Advanced Step 4

Change the interpolator.

SlideInLeftAnimator animator = new SlideInLeftAnimator();
animator.setInterpolator(new OvershootInterpolator());
// or recyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f));
recyclerView.setItemAnimator(animator);

Advanced Step 5

By extending AnimateViewHolder, you can override preset animation.
So, custom animation can be set depending on view holder.

static class MyViewHolder extends AnimateViewHolder {

  public MyViewHolder(View itemView) {
    super(itemView);
  }

  @Override
  public void animateRemoveImpl(ViewPropertyAnimatorListener listener) {
    ViewCompat.animate(itemView)
          .translationY(-itemView.getHeight() * 0.3f)
          .alpha(0)
          .setDuration(300)
          .setListener(listener)
          .start();
  }

  @Override
  public void preAnimateAddImpl() {
    ViewCompat.setTranslationY(itemView, -itemView.getHeight() * 0.3f);
    ViewCompat.setAlpha(itemView, 0);
  }

  @Override
  public void animateAddImpl(ViewPropertyAnimatorListener listener) {
    ViewCompat.animate(itemView)
          .translationY(0)
          .alpha(1)
          .setDuration(300)
          .setListener(listener)
          .start();
  }
}

Animators

Cool

LandingAnimator

Scale

ScaleInAnimatorScaleInTopAnimatorScaleInBottomAnimator
ScaleInLeftAnimatorScaleInRightAnimator

Fade

FadeInAnimatorFadeInDownAnimatorFadeInUpAnimator
FadeInLeftAnimatorFadeInRightAnimator

Flip

FlipInTopXAnimatorFlipInBottomXAnimator
FlipInLeftYAnimatorFlipInRightYAnimator

Slide

SlideInLeftAnimatorSlideInRightAnimatorOvershootInLeftAnimatorOvershootInRightAnimator
SlideInUpAnimatorSlideInDownAnimator

RecyclerView.Adapter

Step 1

Set RecyclerView ItemAnimator.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(new AlphaInAnimationAdapter(adapter));

Advanced Step 2

Change the durations.

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

Advanced Step 3

Change the interpolator.

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

Advanced Step 4

Disable the first scroll mode.

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

Advanced Step 5

Multiple Animations

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

Adapters

Alpha

AlphaInAnimationAdapter

Scale

ScaleInAnimationAdapter

Slide

SlideInBottomAnimationAdapter
SlideInRightAnimationAdapterSlideInLeftAnimationAdapter

Applications using RecyclerView Animators















目录
相关文章
|
12月前
|
搜索推荐 Java 开发工具
RecyclerView的那点事儿
RecyclerView的那点事儿
64 0
|
前端开发 Android开发
RecyclerView实现吸底效果—ItemDecoration
RecyclerView实现吸底效果—ItemDecoration
SwipeRefreshLayout与RecyclerView的巧夺天工(一)
SwipeRefreshLayout与RecyclerView的巧夺天工(一)
66 0
SwipeRefreshLayout与RecyclerView的巧夺天工(二)
SwipeRefreshLayout与RecyclerView的巧夺天工(二)
63 0
SwipeRefreshLayout与RecyclerView的巧夺天工(二)
|
开发工具 Android开发
RecyclerView与CardView的使用(一)
RecyclerView与CardView的使用(一)
126 0
RecyclerView与CardView的使用(一)
RecyclerView与CardView的使用(二)
RecyclerView与CardView的使用(二)
102 0
RecyclerView与CardView的使用(二)
|
存储 缓存 Java
RecyclerView问题汇总
目录介绍 25.0.0.0 请说一下RecyclerView?adapter的作用是什么,几个方法是做什么用的?如何理解adapter订阅者模式? 25.0.0.1 ViewHolder的作用是什么?如何理解ViewHolder的复用?什么时候停止调用onCreateViewHolder? 25.
2953 0
|
异构计算
RecyclerView 数据预取
本文讲的是RecyclerView 数据预取,在我小时候,妈妈为了治疗我的拖延症,总是告诉我:“如果你现在打扫你的房间,就不用以后再打扫了。”但我从没这样做。我知道最好能拖延就尽量拖延。一个原因是:如果我现在打扫了,房间还会变脏,那时候我就必须再打扫一遍了。
2684 0