Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

android - How to use ItemAnimator in a RecyclerView?

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); 

}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...