Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged

In my project I need disable the "change" animation of RecyclerView while notifyItemChanged.

I investigated in the source of RecyclerView and had overridden android.support.v7.widget.DefaultItemAnimator as below:

private static  class ItemAnimator extends DefaultItemAnimator
{
    @Override
    public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {
        if(oldHolder != null)
        {
            oldHolder.itemView.setVisibility(View.INVISIBLE);
            dispatchChangeFinished(oldHolder, true);
        }

        if(newHolder != null)
        {
            dispatchChangeFinished(newHolder, false);
        }

        return false;
    }
}

But I am not sure if I match the spec of the Google document: RecyclerView.ItemAnimator.animateChange

According to my understanding source code, if I do not override the method properly, the oldHolder will not be recycled.

Please help me figure out how to override animateChange in a correct way.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I have found the correct solution to just remove the animateChange.

It's very simple. Google has implemented the functionality.

((SimpleItemAnimator) RecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

Documentation: setSupportsChangeAnimations


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...