【问题标题】:Fragment slide out animation doesn't work片段滑出动画不起作用
【发布时间】:2014-11-19 09:28:59
【问题描述】:

我有一个 android 应用程序,我在其中通过单击按钮从一个片段加载另一个片段

Fragment fragment = new EditImagesFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction tran = fragmentManager.beginTransaction();
tran.setCustomAnimations(R.anim.push, R.anim.pop);
tran.replace(R.id.content_frame, fragment).addToBackStack(null).commit();

我有推送和弹出(滑入和滑出)类型的动画定义如下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="1000"
        android:valueTo="0"
        android:valueType="floatType" />

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="-1000"
        android:valueType="floatType" />

</set>

现在,当我加载片段时,它会按预期滑入,但当我按下后退按钮并尝试返回时滑出动画不起作用,我支持 v14 及以上 API 级别。 有人能发现问题吗?

谢谢

【问题讨论】:

    标签: android animation fragment


    【解决方案1】:

    试试

    setCustomAnimations(int enter, int exit, int popEnter, int popExit)

    而不是

    setCustomAnimations(int enter, int exit)

    根据Android documentationenterexit 动画将不会播放 从返回堆栈弹出。

    【讨论】:

      【解决方案2】:

      您应该使用FragmentTransaction.setCustomAnimations(...) 的4 参数方法而不是2。

      另外,由于您使用原生片段 api(api 14+),我想动画 xmls 应该放在 animator res 文件夹中。

      【讨论】:

      • 它们在“animo”文件夹中,用于 4 参数方法我不需要指定其他动画吗?
      • 请尝试(R.animator.in, R.animator.out, R.animator.in, R.animator.out)
      • tran.setCustomAnimations(R.anim.push, R.anim.pop, R.anim.push, R.anim.pop);是我所做的,现在对于进/出它只在(一个方向)滑动
      • 一个方向是什么意思?
      • 我的意思是它在加载和卸载两次时它只在一个方向上滑动,因为它在一个方向上滑动
      【解决方案3】:

      试试:

       fragmentTransaction.setCustomAnimations(R.anim.enter_new_screen, 0, 0,R.anim.remove_fragment);
      

      进入新屏幕:

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

      remove_fragment:

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

      setcustomanimations 方法在片段的情况下采用四个参数,伙计,我不擅长解释,但这是一种解决方法。试一试,让我知道它是否有效。

      【讨论】:

        猜你喜欢
        • 2018-03-11
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多