动画三(ItemAnimator)

RecyclerView.ItemAnimator类,用于给RecyclerView的item条目添加动画。
RecylerView.ItemAnimator类是个抽象类,系统提供了一个抽象的子类,SimpleItemAnimator,注意这个类也是抽象的。不仅如此,系统还提供了一个DefaultItemAnimator,这个类是继承SimpleItemAnimator类,这个DefaultItemAnimator类提供了默认的RecyclerView的item的动画,下面看看如何使用这个DefaultItemAnimator,实现RecyclerView的Item的动画效果。
1.首先创建布局文件activity_item_animator.xml文件,具体代码如下:



    
        

2.创建ItemAnimatorActivity

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import test.cn.example.com.androidskill.R;

public class ItemAnimatorActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private MyRecyclerAdapter4 adapter4;
    private int position;
    private UIHandler uiHandler = new UIHandler(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_animator);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        recyclerView = findViewById(R.id.recyclerView);
        List data = new ArrayList();
        adapter4 = new MyRecyclerAdapter4(this,data);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter4);

        DefaultItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setAddDuration(500);
        itemAnimator.setRemoveDuration(500);
        recyclerView.setItemAnimator(itemAnimator);

        for(int i=0;i<16;i++){
            uiHandler.sendEmptyMessageDelayed(i,500*(i+1));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                if(1==position){
                    position = 2;
                }else {
                    position = 1;
                }
                adapter4.addData(position,"add item"+new Random().nextInt(100));
                break;
            case R.id.btn_delete:
                adapter4.removeData(1);
                break;
        }
    }

    private class UIHandler  extends Handler{
        WeakReference softReference;
        public UIHandler(Activity activity){
            softReference = new WeakReference(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Activity activity = softReference.get();
            if(null != activity){
                int what = msg.what;
                adapter4.addData(what,"add item "+what);
                if(what == 15){
                    //这里给recyclerView设置ItemAnimator为null,
                    //是为了让后续添加的条目,不在使用这个动画,要是后续添加的 条目想继续使用这个动画,
                    //这行代码可以去掉
                    recyclerView.setItemAnimator(null);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity的onDestory时,清空所有handler中没有执行完的任务,这样在GC就能快速的回收activity
        uiHandler.removeCallbacksAndMessages(null);
    }
}

下面是MyRecyclerAdapter4这个类的具体代码:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import test.cn.example.com.androidskill.R;

public class MyRecyclerAdapter4 extends RecyclerView.Adapter {
    private Context mContext;
    private List mData;
    public MyRecyclerAdapter4(Context context, List data){
        mContext = context;
        mData = data;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recyclerview2, viewGroup,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
        myViewHolder.itemView.setTag(position);
        myViewHolder.tv.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return (null == mData)?0:mData.size();
    }

    public void addData(int position,String itemData) {
        mData.add(position,itemData);
        notifyItemInserted(position);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}

这里是item_recyclerview4的具体代码:



    

这样就完成了一个给RecyclerView的item添加默认的动画的效果。
动画三(ItemAnimator)_第1张图片

这里只是完成了系统提供的默认的动画效果,如果我们想要其它的动画效果,系统又没有提供,该怎么办?当然是要自定义了。下篇文章将在这篇文章的基础上,讲解如何自定义ItemAnimator。

你可能感兴趣的:(android)