Search code examples
androidandroid-recyclerviewandroid-animationandroid-transitions

How to add an action after an explode transition


so I finally figured out how to make an explode transition for a recycleview. What I want to do is to change layout margins AFTER the transition is finished. If I just add the line after the transition is initiated, it doesn't work. So how to set an action to be executed exactly after the transition is done? Here is the code:

recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //Set enormous padding
                int prevPreviousBottomPadding = recyclerView.getPaddingBottom();
                int prevPreviousTopPadding = recyclerView.getPaddingTop();
                recyclerView.setPadding(0,2000,0,2000);


                // save rect of view in screen coordinates
                final Rect viewRect = new Rect();
                //v.getGlobalVisibleRect(viewRect);

                //create Explode transition with epicenter
                Transition explode = new Explode();
                explode.setEpicenterCallback(new Transition.EpicenterCallback() {
                    @Override
                    public Rect onGetEpicenter(Transition transition) {
                        return viewRect;
                    }
                });
                explode.setDuration(800);
                TransitionManager.beginDelayedTransition(recyclerView, explode);
                // remove all views from Recycler View
                recyclerView.setAdapter(null);
                
//I need this line to be executed after the transition is completely over.
                recyclerView.setPadding(0,prevPreviousTopPadding,0,prevPreviousBottomPadding);
                return false;
            }
        }); 

Solution

  • Figured out!! Before we start an animation, we should add a listener and override all the necessary methods. Here is the code:

       recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isAnimating) {
                    return false;
                }
                // save rect of view in screen coordinates
                final Rect viewRect = new Rect();
                //v.getGlobalVisibleRect(viewRect);
    
                //Set enormous padding
                int prevPreviousBottomPadding = recyclerView.getPaddingBottom();
                int prevPreviousTopPadding = recyclerView.getPaddingTop();
                recyclerView.setPadding(0, 2000, 0, 2000);
    
                //create Explode transition with epicenter
                Transition explode = makeInExplodeTransition();
                explode.setEpicenterCallback(new Transition.EpicenterCallback() {
                    @Override
                    public Rect onGetEpicenter(Transition transition) {
                        return viewRect;
                    }
                });
    
    //Here it is - adding a listener
                explode.addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(Transition transition) {
                        isAnimating = true;
                    }
    
                    @Override
                    public void onTransitionEnd(Transition transition) {
                        isAnimating = false;
                        recyclerView.setPadding(0, prevPreviousTopPadding, 0, prevPreviousBottomPadding);
                    }
    
                    @Override
                    public void onTransitionCancel(Transition transition) {
    
                    }
    
                    @Override
                    public void onTransitionPause(Transition transition) {
    
                    }
    
                    @Override
                    public void onTransitionResume(Transition transition) {
    
                    }
                });
                TransitionManager.beginDelayedTransition(recyclerView, explode);
                //Deleting everything from our adapter
                recyclerView.setAdapter(null);
                return false;
            }
        });