Animate the transition between fragments

前端 未结 8 1098
梦如初夏
梦如初夏 2020-11-22 14:59

I\'m trying to animate the transition between fragments. I got the answer from the following
Android Fragments and animation

FragmentTransaction ft = g         


        
相关标签:
8条回答
  • 2020-11-22 15:22

    If you can afford to tie yourself to just Lollipop and later, this seems to do the trick:

    import android.transition.Slide;
    import android.util.Log;
    import android.view.Gravity;
    .
    .
    .
    f = new MyFragment();
    f.setEnterTransition(new Slide(Gravity.END));
    f.setExitTransition(new Slide(Gravity.START));
    getFragmentManager()
        .beginTransaction()
        .replace(R.id.content, f, FRAG_TAG)  // FRAG_TAG is the tag for your fragment
        .commit();
    

    Kotlin version:

    f = MyFragment().apply {
        enterTransition = Slide(Gravity.END)
        exitTransition = Slide(Gravity.START)
    }
    fragmentManager
        .beginTransaction()
        .replace(R.id.content, f, FRAG_TAG)  // FRAG_TAG is the tag for your fragment
        .commit();
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 15:29

    For anyone else who gets caught, ensure setCustomAnimations is called before the call to replace/add when building the transaction.

    0 讨论(0)
提交回复
热议问题