0

I successfully added onPreDraw animations to a RecyclerView using the following code. I call this function immediately after the adapter is set.

public void initialRVAnimation() {

    rvAnims =  new ViewTreeObserver.OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    rv.getViewTreeObserver().removeOnPreDrawListener(this);
                    for (int i = 0; i < rv.getChildCount(); i++) {
                        View v = rv.getChildAt(i);
                        v.setTranslationY(Utils.getScreenHeight(LiveThreadActivity.this));
                        v.animate()
                                .setStartDelay(50 * i)
                                .translationY(0)
                                .setInterpolator(new DecelerateInterpolator(3.f))
                                .setDuration(700)
                                .start();
                    }

                    return true;
                }

    };

    rv.getViewTreeObserver().addOnPreDrawListener(rvAnims);
}

However, this causes an issue when adding items to the RecyclerView. The items seem to inherit the start delay specified in the animation and when a new item is added to the top of the RecyclerView, rather than all the items below moving down at once, they move in a staggered way. Here's a video to demonstrate the problem. Here's the normal animation when adding items when not using the onPreDraw animation.

Items are added to the RecyclerView using the following code.

public void addItem(View v) {
    data.add(1, new Comment());
    adapter.notifyItemInserted(1);
}
Vishnu M.
  • 971
  • 1
  • 10
  • 18
  • use `RecyclerView.ItemAnimator` – pskink Aug 13 '16 at 19:01
  • Issue resolved using this method: http://stackoverflow.com/questions/38909542/how-to-animate-recyclerview-items-when-adapter-is-initialized-in-order/38937064#38937064 – Vishnu M. Aug 13 '16 at 21:23

0 Answers0