碎片回退栈动画无法正常工作

5

我想在打开和关闭时对一个片段进行动画处理。我有一个淡入淡出的自定义动画XML文件。

我正在使用support 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时,在滑入之前才显示淡出动画,但从未在调用fragmentDisappear时显示。我已经尝试了许多不同的参数顺序,还尝试了setCustomAnimations的两个参数版本,但所有这些都只会在片段出现时进行动画。

我正在使用android.support.v4.app库来管理我的片段。

编辑:此外,如果没有调用fragmentDisappear,按下后退按钮时动画也不会显示。

过去的代码在活动中,我尝试从片段中执行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>
3个回答

8

如果你查看代码,你是用新的fragment来替换旧的fragment,但实际上你将添加到返回堆栈的标签设置为null。为每个fragment提供一个标记是良好的编程习惯,通过标记查找该fragment也将变得更容易。像下面这样为你的fragment添加标记。如果仍然不起作用,则问题可能在你的动画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 => 从后台堆栈弹出时片段退出的动画。

尝试调整这些选项,直到达到所需效果。


非常感谢。在调用popBackStack时,我应该将片段标记设置为参数吗?在“flags”中应该写什么? - sergiotbh
@SergioYáñezJiménez => 标签只是一个标签,就像价格标签一样。它可以是任何字符串,您将来可以使用它来定位/查找此片段。是的,请将其作为参数传递。 - CodeDaily
是的,但popBackStack需要两个参数...无论如何,我选择了这个popBackStack("loginfragment", FragmentManager.POP_BACK_STACK_INCLUSIVE); - sergiotbh
还是不行...我在最新的编辑中包含了我的XML文件。谢谢。 - sergiotbh
@SergioYáñezJiménez => 我已更新我的回答,请查看。 - CodeDaily
显示剩余2条评论

1
在我的情况下,片段容器的高度是wrap_content,因此弹出动画无法正常工作(而进入动画可以正常工作)。将片段容器的高度设置为特定高度或match_parent将使弹出动画正常工作。

-1

这段代码对我来说是有效的。如果你想在活动中使用这段代码,请删除开头的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();

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