Android番外篇 RecyclerView 移除飞行效果动画

Posted 彭老希

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android番外篇 RecyclerView 移除飞行效果动画相关的知识,希望对你有一定的参考价值。

一、移除飞行动画代码

import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Dumplings
 * @version : v1.0.0
 * @ClassName : FlyAnimator.java
 * @Function : 移除飞行效果动画
 * @Description :
 * @Idea :
 * {@link  }
 * @Encourage :And the more we try to understand one another, the more exceptional each of us will be.
 * 我们越是努力理解他人,自身也越发变得优秀。
 * @date : 2021/6/3
 */
public class FlyAnimator extends SimpleItemAnimator{
    List<RecyclerView.ViewHolder> removeHolders = new ArrayList<>();
    List<RecyclerView.ViewHolder> removeAnimators = new ArrayList<>();
    List<RecyclerView.ViewHolder> moveHolders = new ArrayList<>();
    List<RecyclerView.ViewHolder> moveAnimators = new ArrayList<>();
    @Override
    public boolean animateRemove(RecyclerView.ViewHolder holder) {
        removeHolders.add(holder);
        return true;
    }
    @Override
    public boolean animateAdd(RecyclerView.ViewHolder holder) {
        return false;
    }
    @Override
    public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY) {
        holder.itemView.setTranslationY(fromY - toY);
        moveHolders.add(holder);
        return true;
    }
    @Override
    public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop) {
        return false;
    }
    @Override
    public void runPendingAnimations() {
        if(!removeHolders.isEmpty()) {
            for(RecyclerView.ViewHolder holder : removeHolders) {
                remove(holder);
            }
            removeHolders.clear();
        }
        if(!moveHolders.isEmpty()){
            for(RecyclerView.ViewHolder holder : moveHolders) {
                move(holder);
            }
            moveHolders.clear();
        }
    }
    @Override
    public void endAnimation(RecyclerView.ViewHolder item) {
    }
    @Override
    public void endAnimations() {
    }
    @Override
    public boolean isRunning() {
        return !(removeHolders.isEmpty() && removeAnimators.isEmpty() && moveHolders.isEmpty() && moveAnimators.isEmpty());
    }

    private void remove(final RecyclerView.ViewHolder holder){
        removeAnimators.add(holder);
        TranslateAnimation animation = new TranslateAnimation(0, 1000, 0, 0);
        animation.setDuration(500);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                dispatchRemoveStarting(holder);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                removeAnimators.remove(holder);
                dispatchRemoveFinished(holder);
                if(!isRunning()){
                    dispatchAnimationsFinished();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        holder.itemView.startAnimation(animation);
    }

    private void move(final RecyclerView.ViewHolder holder){
        moveAnimators.add(holder);
        ObjectAnimator animator = ObjectAnimator.ofFloat(holder.itemView,
                "translationY", holder.itemView.getTranslationY(), 0);
        animator.setDuration(500);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(android.animation.Animator animation) {
                dispatchMoveStarting(holder);
            }

            @Override
            public void onAnimationEnd(android.animation.Animator animation) {
                dispatchMoveFinished(holder);
                moveAnimators.remove(holder);
                if(!isRunning()) {
                    dispatchAnimationsFinished();
                }
            }
        });
        animator.start();
    }

}

二、调用

		private RecyclerView recyclerView;
		private LockedApplicationAdapter mAdapter;
		
		recyclerView = this.findViewById(R.id.recyclerView);
		mAdapter = new XXXXXXAdapter(getAttachActivity());
        /** 移除动画 */
        recyclerView.setItemAnimator(new FlyAnimator());
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(mAdapter);
        //传入的数据,也可以通过new XXXXXXAdapter的构造方法传入
        mAdapter.setData(null);

以上是关于Android番外篇 RecyclerView 移除飞行效果动画的主要内容,如果未能解决你的问题,请参考以下文章

单例模式番外篇

Android番外篇 AndroidStudio独立运行Module

Android番外篇 LruCache缓存机制

Apache Cordova开发Android应用程序——番外篇

Android番外篇 Glide获取图片PathBitmap用法详解

Android番外篇 Android Studio 右上角Gradle不显示 Task