简体   繁体   English

方向更改时片段Z顺序更改

[英]Fragment Z-order changes when orientation changes

My main xml file has a FrameLayout, with one fragment defined in the XML. 我的主要xml文件具有一个FrameLayout,在XML中定义了一个片段。 At runtime, I am adding another fragment on top of this, and then a third fragment on top of this. 在运行时,我在此之上添加了另一个片段,然后在此之上添加了第三个片段。 When I add the runtime fragments, I don't give containerIds to attach them to. 当我添加运行时片段时,我没有给containerIds附加它们。 This is all fine until the orientation changes. 直到方向改变,一切都很好。 After the orientation change, the third fragment has gone beneath the second fragment. 方向更改后,第三个片段已位于第二个片段之下。 But obviously I want the z-order to stay how it was before the orientation change, ie the last fragment added should be on top and visible. 但是显然我希望z顺序保持方向更改之前的状态,即最后添加的片段应该在顶部并可见。

I've tried using containerIds when adding the fragments, and adding them to the FrameLayout defined in XML, but when I put in containerIds, the runtime fragments simply don't show up at all. 在添加片段并将其添加到XML定义的FrameLayout中时,我曾尝试使用containerIds,但是当我放入containerIds时,运行时片段根本不会显示。

So I'm wondering why the fragments added at runtime don't show when I use a containerId. 所以我想知道为什么当我使用containerId时,在运行时添加的片段不显示。 Is it because they are being added to a container which already contains a fragment (the one defined in XML)? 是否因为将它们添加到已经包含片段(以XML定义的片段)的容器中? How can I get around this? 我该如何解决? I've tried adding additional containers to the XML to hold the runtime fragments, but this doesn't seem to work. 我尝试将其他容器添加到XML中以保存运行时片段,但这似乎不起作用。 The only way to get the fragments to show is to remove the containerId from the call to FragmentTransaction.add(). 要显示片段的唯一方法是从FragmentTransaction.add()的调用中删除containerId。

Also, is it possible to change the z-order of fragments manually? 另外,是否可以手动更改片段的z顺序? I've searched Google quite a lot to find this out and haven't found any answers. 我已经在Google上进行了大量搜索,以找出答案,但未找到任何答案。 I've tried using ViewGroup.bringChildToFront() and using the view returned by the fragment's getView() but this hasn't worked. 我尝试使用ViewGroup.bringChildToFront()并使用该片段的getView()返回的视图,但这没有用。 I've also found out that you can nest fragments on Android 4.2+ with getChildFragmentManager(), but I'm supporting older APIs, and this method doesn't seem to be in the support library. 我还发现您可以使用getChildFragmentManager()在Android 4.2+上嵌套片段,但是我支持较旧的API,并且该方法似乎不在支持库中。

Any help greatly appreciated! 任何帮助,不胜感激!

我解决这个问题的方法是在方向更改后手动重新创建第三个片段,并删除系统创建的片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM