Android - 片段事务上的自定义动画未运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11041325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:50:24  来源:igfitidea点击:

Android - Custom Animation on fragment transaction not running

androidandroid-fragmentsandroid-animation

提问by adheus

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

我正在使用带有支持包 v4 的 Google API 8 (Android 2.2)。

It doesn't give any error or animation.

它不会给出任何错误或动画。

Transaction:

交易:

FragmentTransaction transaction = manager.beginTransaction();       
transaction.replace(R.id.content, myFragment);
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
transaction.commit();

Animations:

动画:

slide_in_left.xml

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="700"
        android:fromXDelta="-100%"
        android:toXDelta="0%" >
    </translate>
</set>

slide_out_right.xml

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:toXDelta="100%" >
    </translate>
</set>

Does anyone know what is happening here?

有谁知道这里发生了什么?

回答by adheus

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().

经理在我设置动画之前堆叠了我的事务,所以它在没有动画的情况下堆叠了事务(悲伤但真实),即使我在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();

回答by Harisewak

As suggested above, separate statements will definitely work. But the trick hereis to setCustomAnimationbefore setting transaction type viz.add, replace, etc. else it doesn't. So, applying the same logic, method chainingalso works. eg.

如上所述,单独的语句肯定会起作用。但这里技巧setCustomAnimation在设置事务类型之前。add, replace, 等等,否则它不会。因此,应用相同的逻辑method chaining也有效。例如。

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 chainingfinds it helpful. Cheers!

把它放在这里,以便喜欢的人method chaining发现它有帮助。干杯!

回答by Màrius Mora Bosch

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:animateLayoutChangesset to truein the containing layout.

把这个留在这里,因为它是最受欢迎的问题。我在片段事务没有动画时遇到了同样的问题。罪魁祸首是在包含的布局中将属性android:animateLayoutChanges设置为true

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.

我希望它可以帮助人们节省一些寻找解决方案的时间,因为在不同文件中嵌套布局时很难注意到。

回答by Carson Holzheimer

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

另一个原因可能是fragmentTransaction.show()在提交之前不必要地放置。这使得某些 android API 版本上不会显示 pop 转换。