【问题标题】:What is the difference between enter/exit and popEnter/popExit animations?enter/exit 和 popEnter/popExit 动画有什么区别?
【发布时间】:2017-04-14 05:06:25
【问题描述】:

setCustomAnimations() 中,动画需要四个资源ID。不是很了解他们。如果有人对它有更清晰的了解,如果您能解释一下,将不胜感激。

假设将片段 A 添加到占位符和 backstack 中。

FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.holder, fragA, FragmentA.FRAGMENT_NAME);
        ft.addToBackStack(FragmentA.FRAGMENT_NAME);
        ft.setCustomAnimations(R.anim.slide_in_from_bottom, R.anim.slide_in_from_top, R.anim.slide_in_from_left, R.anim.slide_in_from_right);
        ft.show(frag);
        ft.commit();

然后用片段 B 替换:

FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.holder, fragB, FragmentB.FRAGMENT_NAME);
        ft.addToBackStack(FragmentB.FRAGMENT_NAME);
        ft.setCustomAnimations(R.anim.slide_in_from_bottom, R.anim.slide_in_from_top, R.anim.slide_in_from_left, R.anim.slide_in_from_right);
        ft.show(frag);
        ft.commit();

下次如果做一个popstack()

fm.popBackStackImmediate(FragmentB.FRAGMENT_NAME,
                                FragmentManager.POP_BACK_STACK_INCLUSIVE);

它将从哪个事务的动画开始运行?

/**
 * 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);

【问题讨论】:

    标签: android fragmenttransaction fragment-backstack


    【解决方案1】:

    让我们开始简单的案例:

    用片段 B 替换片段 A(你的第二个代码 sn-p)

    • 片段 B 运行进入动画
    • 片段 A 运行退出动画

    按返回键,撤消替换操作

    • 片段 B 运行 popExit 动画
    • 片段 A 运行 popEnter 动画

    现在回答你的问题。

    你没有说容器是否已经有一个片段。让我们考虑这两种情况:

    1. 在调用第一个用片段 A 替换的操作时,容器已经有一个片段(我们称之为片段 0)。弹出整个堆栈时:

      • 片段 B 运行 popExit 动画(在第二个 sn-p 中设置)
      • 片段 0 运行 popEnter 动画(在第一个 sn-p 中设置)
    2. Container 是空的,所以用 Fragment A 替换本质上是一个添加操作。弹出整个堆栈时:

      • 片段 B 运行 popExit 动画(在第二个 sn-p 中设置)
      • 没有 popEnter 动画运行,因为容器现在是空的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-28
      • 2016-12-08
      • 2015-03-29
      • 2014-10-08
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      相关资源
      最近更新 更多