androidandroid-fragmentsandroid-animationandroid-transitionsfragment-transitions

Make fragment transition animate views simultaneously


I am replacing a fragment A with a fragment B and animating this. Fragment B has a RelativeLayout with a TextView and a ListView. What I could see is that the views in fragment B are being animated separately, the TextView slides in with a different velocity than the list item 1 subtitle, while list item 1 title and icon appear instantly without animation, I would like the whole view to animate at the same time, is this possible to achieve?

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment existing = fragmentManager.findFragmentById(R.id.welcome_content);
    if (existing != null) {                
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            fragmentToLoad.setAllowEnterTransitionOverlap(true);                    
            fragmentToLoad.setEnterTransition(new Slide(Gravity.RIGHT));
        }
        fragmentTransaction.remove(existing);
    }
    fragmentTransaction.add(R.id.welcome_content, fragmentToLoad);
    fragmentTransaction.commitNow();

Solution

  • Apply android:transitionGroup="true" to the root layout of FragmentB.

    From docs of ViewGroup#setTransitionGroup(boolean):

    Changes whether or not this ViewGroup should be treated as a single entity during Activity Transitions. If false, the ViewGroup won't transition, only its children. If true, the entire ViewGroup will transition together.