FragmentTransation的setCustomAnimations方法不起作用

30

我正在尝试使用新的Android兼容性包将片段包含到我的项目中。我想在添加新片段时包含一个过渡动画。但问题是只有其中一个动画能够正常工作。进入动画可以工作,但退出动画无法工作。 我在某个地方读到,这是兼容性包中的一个错误。但我也看到说这个错误已经在兼容性包的第三个修订版中得以修复。有谁能帮我解决这个问题吗?

进入动画

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/anticipate_interpolator"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="100%"        
android:toYDelta="0%"
android:duration="1000"/>

退出动画

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="0"
android:zAdjustment="top"
android:fromYDelta="0%"        
android:toYDelta="100%"
android:duration="1000"/>

这是我用来添加片段的代码

newFragment = new HelloWorldFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.bottom_top_animation, R.anim.top_bottom_animation);
ft.add(R.id.outer_layout, newFragment);
ft.addToBackStack(null);
ft.commit();

第3版中没有有关解决动画问题的信息:http://developer.android.com/sdk/compatibility-library.html#Notes - pawelzieba
我当前放弃了片段,因为我认为它在大型项目中还不够成熟,无法实现。 - blessanm86
4个回答

23

这在当前版本的库中可行,但之前肯定是有问题的。可以使用类似这样的代码:

final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down, R.anim.slide_up, R.anim.slide_down)
  .add(R.id.fragment_container, new SomeFragment(), FRAGMENT_TAG)
  .addToBackStack(FRAGMENT_TAG)
  .commit();

其中R.anim.slide_up是您的进入动画,R.anim.slide_down是您的退出动画。setCustomAnimations的第二组参数(3和4)允许您指定弹出后退堆栈时的弹出/弹入动画(例如,当用户按下返回键时,片段将使用指定为第四个参数的动画进行动画处理)。


7
它给我返回了“未知动画师名称:translate... error”的错误信息,你知道我是否漏掉了什么吗? - CoDe
6
我的滑动下降动画似乎没有起作用。相反,该片段只是被移除,并且没有滑动动画而消失了。 - Etienne Lawlor
1
@MAGx2:您可以使用setCustomAnimations与getSupportFragmentManager()。不过在API 11之后,必须使用ObjectAnimators,通常存储在R.animator中,而非translate animators。 - Lo-Tan
2
请注意,在当前版本的支持库(21.0.2)中,您无法使用“setCustomAnimations”。这是一个已知问题:http://code.google.com/p/android/issues/detail?id=77670。 - android developer
16
对于'com.android.support:support-v4:21.0.3'版本,动画可以正常工作,但是在添加/替换之前调用setCustomAnimations非常重要。 - stefan.nsk
显示剩余4条评论

8
我已经找到了解决方法。在您的片段类中覆盖onCreateAnimation(int transit, boolean enter, int nextAnim)方法,那么它就能正常工作了。
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    return enter ? AnimationUtils.loadAnimation(getActivity(), R.anim.grow_fade_in_center) : AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_fade_out_center);
}

你是指 'onCreateAnimator' 吗? - Gallal
方法名称与上面的代码片段中提到的相同,它的片段方法只是覆盖了这个。 - Mohit
退出动画对我仍然无效。首个片段被替换,然后播放其进入动画,但是上一个片段的退出动画未被播放。 - Dhruvam Gupta
transit和nextAnim参数始终为0! - Mohammad Afrashteh

1
我也遇到了同样的问题,这是我的情况:
1. 我想在退出动画后进行一些 UI 更改,但我发现退出动画不起作用。我的 UI 更改代码在 onBackPressed() 中。
我的解决方案如下:
1. 将 UI 更改逻辑移至 onCreateAnimator() 中。然后退出动画就能正常工作了。

0
使用以下示例:
 private fun gotoFragment(fragment:Fragment){
     parentFragmentManager.commit {
        setCustomAnimations(
             R.anim.slide_in,
           R.anim.fade_out,
              R.anim.fade_in,
             R.anim.slide_out
        )
         setReorderingAllowed(true)
        replace(R.id.fragment_dashboard_container, fragment)
        addToBackStack(null)
    }
}

这里是文档 片段事务


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接