【问题标题】:Animations while opening/ closing a FragmentAdapter打开/关闭 FragmentAdapter 时的动画
【发布时间】:2013-06-19 23:25:49
【问题描述】:

我想在将片段附加到包含 FragmentPager 的 Activity 时播放自定义动画。不幸的是,使用FragmentTransaction.setCustomAnimations(int, int) 的正常方式只能部分起作用。无论我做什么,都会显示过渡动画或过渡动画。

这是我目前所拥有的:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.fadein, R.anim.fadeout, R.anim.fadein, R.anim.fadeout);
    transaction.add(R.id.fragmentSpace, hostingFragment, CURRENT_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();

在这种情况下,只显示新片段的动画,旧片段在新附加的片段下保持可见。再加上弹出回栈时不播放动画。

transaction.add(..) 替换为transaction.replace(..) 有助于删除底层Fragment,但仍然存在未显示其出动画的问题。在这种情况下,底层的 Fragment 就消失了。至少弹出动画显示在 .replace 案例中。

示例项目参见:github(动画分支)

【问题讨论】:

    标签: android android-fragments android-animation


    【解决方案1】:

    您应该在片段事务上使用remove()replace() 以摆脱另一个片段。

    【讨论】:

    • 正如我上面提到的:transaction.replace() 也无济于事。它无法显示替换片段的出动画。刚刚测试了组合 remove(Fragment) 和 add(Fragment) 但它实际上与调用 replace 相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    相关资源
    最近更新 更多