嵌套片段在过渡动画期间消失 [英] Nested fragments disappear during transition animation

查看:20
本文介绍了嵌套片段在过渡动画期间消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景如下:Activity包含片段A,它依次使用getChildFragmentManager()添加片段A1A2 在它的 onCreate 像这样:

Here's the scenario: Activity contains fragment A, which in turn uses getChildFragmentManager() to add fragments A1 and A2 in its onCreate like so:

getChildFragmentManager()
  .beginTransaction()
  .replace(R.id.fragmentOneHolder, new FragmentA1())
  .replace(R.id.fragmentTwoHolder, new FragmentA2())
  .commit()

到目前为止,一切都很好,一切都按预期运行.

So far, so good, everything is running as expected.

然后我们在 Activity 中运行以下事务:

We then run the following transaction in the Activity:

getSupportFragmentManager()
  .beginTransaction()
  .setCustomAnimations(anim1, anim2, anim1, anim2)
  .replace(R.id.fragmentHolder, new FragmentB())
  .addToBackStack(null)
  .commit()

在过渡期间,片段 Benter 动画运行正常,但 片段 A1 和 A2 完全消失.当我们使用 Back 按钮恢复事务时,它们会正确初始化并在 popEnter 动画期间正常显示.

During the transition, the enter animations for fragment B runs correctly but fragments A1 and A2 disappear entirely. When we revert the transaction with the Back button, they initialize properly and display normally during the popEnter animation.

在我的简短测试中,它变得更奇怪 - 如果我为子片段设置动画(见下文),当我们添加片段 B 时,exit 动画会间歇性地运行

In my brief testing, it got weirder - if I set the animations for the child fragments (see below), the exit animation runs intermittently when we add fragment B

getChildFragmentManager()
  .beginTransaction()
  .setCustomAnimations(enter, exit)
  .replace(R.id.fragmentOneHolder, new FragmentA1())
  .replace(R.id.fragmentTwoHolder, new FragmentA2())
  .commit()

我想要实现的效果很简单——我想要exit(或者应该是popExit?)片段A上的动画(anim2) 运行,动画整个容器,包括其嵌套的子级.

The effect I want to achieve is simple - I want the exit (or should it be popExit?) animation on fragment A (anim2) to run, animating the whole container, including its nested children.

有什么办法可以实现吗?

Is there any way to achieve that?

编辑:请在此处

Edit2:感谢@StevenByle 推动我继续尝试静态动画.显然你可以在每个操作的基础上设置动画(不是整个事务的全局),这意味着孩子可以有一个无限期的静态动画集,而他们的父母可以有不同的动画,整个事情可以在一个事务中提交.请参阅下面的讨论和更新的测试用例项目.

Edit2: Thanks to @StevenByle for pushing me to keep trying with the static animations. Apparently you can set animations on a per-op basis (not global to the whole transaction), which means the children can have an indefinite static animation set, while their parent can have a different animation and the whole thing can be committed in one transaction. See the discussion below and the updated test case project.

推荐答案

为了避免用户在事务中移除/替换父片段时看到嵌套片段消失,您可以模拟"那些仍然存在的片段提供他们出现在屏幕上的图像.该图像将用作嵌套片段容器的背景,因此即使嵌套片段的视图消失,图像也会模拟它们的存在.此外,我不认为失去与嵌套片段视图的交互性是一个问题,因为我认为您不希望用户在它们刚刚被删除时对它们采取行动(可能作为用户操作作为嗯).

In order to avoid the user seeing the nested fragments disappearing when the parent fragment is removed/replaced in a transaction you could "simulate" those fragments still being present by providing an image of them, as they appeared on the screen. This image will be used as a background for the nested fragments container so even if the views of the nested fragment go away the image will simulate their presence. Also, I don't see loosing the interactivity with the nested fragment's views as a problem because I don't think you would want the user to act on them when they are just in the process of being removed(probably as a user action as well).

我做了一个小例子来设置背景图像(一些基本的东西).

I've made a little example with setting up the background image(something basic).

这篇关于嵌套片段在过渡动画期间消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆