Search code examples
javaandroidandroid-studioanimationtransition

Android : Start a transition after another one ends


I have a transition applied to several buttons where I try to change their width from 0dp to MATCH_PARENT

final ChangeBounds transition = new ChangeBounds();
startFloat.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        for(i=0;i<allCards.size();i++) {
            if(i%2 == 0) {
                transition.setDuration(4000L);
                TransitionManager.beginDelayedTransition(allCards.get(i), transition);
                allButton.get(i).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp10));
            }
            else{
                transition.setDuration(400L);
                TransitionManager.beginDelayedTransition(allCards.get(i), transition);
                allButton.get(i).setLayoutParams(new MaterialCardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp10));
            }
        }
    }
});

Note: dp10 is an integer variable (No problem there).

This seems to work fine, however, when I click startFloat button, all the transitions occur at the same time. I want them to start after the previous one ends.

Any idea of how to achieve the same?


Solution

  • I have faced the same problem few days ago. The solution is , you need to use different transitions for different views. You need to create two transitions here and use each of them once only. Try this

    final ChangeBounds firestTransition = new ChangeBounds();
    final ChangeBounds secondTransition= new ChangeBounds();
    
    startFloat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
            for(i=0;i<allCards.size();i++) {
                if(i%2 == 0) {
                    firestTransition.setDuration(4000L);
                    TransitionManager.beginDelayedTransition(allCards.get(i), transition);
                    allButton.get(i).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp10));
                }
                else{
                    secondTransition.setDuration(400L);
                    TransitionManager.beginDelayedTransition(allCards.get(i), transition);
                    allButton.get(i).setLayoutParams(new MaterialCardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp10));
                }
            }
        }
    });