Android - Custom Animation on fragment transaction not running

后端 未结 4 872
梦谈多话
梦谈多话 2020-12-12 19:00

I\'m using Google API 8 (Android 2.2) with support package v4.

It doesn\'t give any error or animation.

Transaction:

Fragme         


        
相关标签:
4条回答
  • 2020-12-12 19:18

    Another reason can be unnecessarily placing fragmentTransaction.show() before commit. This makes pop transitions not show on some android API versions.

    0 讨论(0)
  • 2020-12-12 19:22

    Leaving this here as it's the most popular question. I had the same problem with fragment transaction not animating. The culprit was having the attribute android:animateLayoutChanges set to true in the containing layout.

    I hope it helps someone save some time looking for a solution as it can be hard to notice when having nested layouts in different files.

    0 讨论(0)
  • 2020-12-12 19:25

    As suggested above, separate statements will definitely work. But the trick here is to setCustomAnimation before setting transaction type viz.add, replace, etc. else it doesn't. So, applying the same logic, method chaining also works. eg.

    getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.a_slide_up,
                                 R.anim.a_slide_down,
                                 R.anim.a_slide_up,
                                 R.anim.a_slide_down)
            .add(R.id.root_layout, 
                 MyFrag.newInstance())
            .addToBackStack("MyFrag")
            .commit();
    

    Putting it here, so that someone who prefers method chaining finds it helpful. Cheers!

    0 讨论(0)
  • 2020-12-12 19:27

    The manager was stacking my transaction before I set the animation, so it stacks the transaction without animations (sad but true), and that occurs even if I commit the transaction after the setCustomAnimations().

    The solution is to set the animations first:

    FragmentTransaction transaction = manager.beginTransaction();       
    transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
    transaction.replace(R.id.content, myFragment);
    transaction.commit();
    
    0 讨论(0)
提交回复
热议问题