片段 popbackstack 动画不起作用

Fragment popbackstack animation not working

我想在打开和关闭时为 片段 制作动画。我有淡入和淡出自定义动画 XML 个文件。

我在我的支持 FragmentTransaction 上使用 setCustomAnimations,但它所做的只是在我执行 addToBackStack 时设置动画,而当我执行 popBackStack 时它只是消失而没有动画。

这是我的代码片段:

private void fragmentAppear(){
    fragment = new LoginFragment();
    fragmentManager = LoginActivity.this.getSupportFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();
    //my XML anim files
    fragmentTransaction.setCustomAnimations(R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
    fragmentTransaction.replace(R.id.login_fragment, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

private void fragmentDisappear(){
    getSupportFragmentManager().popBackStack();
}

在 setCustomAnimations 部分,我使用了 4 个参数,到目前为止,当我调用 fragmentAppear 时,它只显示幻灯片之前的淡出动画,但 never 调用时片段消失。我已经尝试过以许多不同的方式对参数进行排序,我也尝试过使用 setCustomAnimations 的两个参数版本,它所做的只是在片段出现时设置动画。

我正在为我的片段使用 android.support.v4.app 库。

编辑:另外,在没有调用 fragmentDisappear 的情况下按下后退按钮时动画不会显示。

过去的代码在 activity 中,我试图从片段中执行 popBackStack,但它也不起作用。这是关闭我的片段的正确方法吗?

编辑:我将包含 XML 动画:

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="75%p"
        android:toYDelta="0%p"
        android:fillAfter="true"
        android:duration="400" />
</set>

slide_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="75%p"
        android:fillAfter="true"
        android:duration="400" />
</set>

如果您查看代码,您正在用新片段替换该片段,但实际上您将添加到返回堆栈设置为 null。为每个片段提供一个标签是一种很好的做法,甚至可以很容易地通过标签找到该片段。将标签添加到您的片段,如下所示。如果它仍然不起作用,那么问题将出在您的动画 xml 文件中。

private void fragmentAppear(){
   fragment = new LoginFragment();
   fragmentManager = LoginActivity.this.getSupportFragmentManager();
   fragmentTransaction = fragmentManager.beginTransaction();
   //my XML anim files
   fragmentTransaction.setCustomAnimations(
        R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
   fragmentTransaction.replace(
        R.id.login_fragment, fragment, "loginFragment");
   fragmentTransaction.addToBackStack("loginFragment");
   fragmentTransaction.commit();
}

从 Fragment Transaction 文档中,我看到了这个函数,您必须在其中指定适当的动画。

/**
 * Set specific animation resources to run for the fragments that are
 * entering and exiting in this transaction. The
 * <code>popEnter</code>
 * and <code>popExit</code> animations will be played for enter/exit
 * operations specifically when popping the back stack.
 */
 public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
        @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit);
  1. enter => 片段进入时的动画
  2. exit => 片段退出时的动画。
  3. popEnter => 片段从返回堆栈进入时的动画。
  4. popExit => 从返回堆栈弹出时片段退出时的动画。

玩这些直到你得到你想要的行为。

这段代码对我有用。如果要在activity中使用这段代码,删除开头的getActivity():

getActivity().getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.fade_out,android.R.anim.slide_in_left, android.R.anim.fade_out)
            .replace(R.id.fragment_container, new YourFragment)
            .addToBackStack(null)
            .commit();

在我的例子中,片段容器的高度是 wrap_content,所以弹出动画不起作用(而进入动画效果很好)。
按特定高度设置片段容器高度或match_parent将使弹出动画正常工作