如何在 RecyclerView 中使用 ItemAnimator? [英] How to use ItemAnimator in a RecyclerView?

查看:14
本文介绍了如何在 RecyclerView 中使用 ItemAnimator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从 recyclerview 的适配器中添加或删除项目时,我想制作动画.我正在尝试按如下方式使用 RecyclerView.ItemAnimator 但它不起作用..

I want to do animation when an item is added or removed from adapter of recyclerview. I'am trying to use RecyclerView.ItemAnimator as follows but it is not working..

    public class MyAnimator extends RecyclerView.ItemAnimator{

    @Override
    public boolean animateAdd(ViewHolder arg0) {
        Log.d("test","Added Animation");
        return false;
    }

    @Override
    public boolean animateChange(ViewHolder arg0, ViewHolder arg1, int arg2, int arg3, int arg4, int arg5) {
        Log.d("test","Change Animation");
        return false;
    }

    @Override
    public boolean animateMove(ViewHolder arg0, int arg1, int arg2, int arg3, int arg4) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean animateRemove(ViewHolder arg0) {
        Log.d("test", "Remove Animation");
        return false;
    }

    @Override
    public void endAnimation(ViewHolder arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void endAnimations() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isRunning() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void runPendingAnimations() {
        // TODO Auto-generated method stub

    }

}

我使用上面的代码如下.

I'am using above code as follows.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    data=new ArrayList<String>();   


    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    animator = new MyAnimator();
    mRecyclerView.setItemAnimator(animator);            

    mAdapter = new MyAdapter(data);
    mRecyclerView.setAdapter(mAdapter); 

}

每当我将新项目添加到数据集并调用 mAdapter.notifyDataSetChanged() 时,我都希望在 MyAnimator 类中存在 animateAdd(ViewHolder arg0) 中的日志;但它没有来..我错过了什么..为什么它不起作用.

Whenever i add a new item into dataset and call mAdapter.notifyDataSetChanged() i expected the Log present in animateAdd(ViewHolder arg0) present in MyAnimator class; but it's not coming..am i missing something..Why its not working.

推荐答案

首先,您不需要自定义 ItemAnimator.你可以使用默认的,去掉 mRecyclerView.setItemAnimator(animator);

First of all, you don't need custom ItemAnimator for that. You can use default one, remove mRecyclerView.setItemAnimator(animator);

此外,您必须使用 notifyItemInserted()notifyItemRemoved() 而不是 notifyDataSetChanged(),它会为您调用适当的动画.

Also, you have to use notifyItemInserted() and notifyItemRemoved() instead of notifyDataSetChanged(), it invokes proper animation for you.

这篇关于如何在 RecyclerView 中使用 ItemAnimator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆