我可以删除在layout.xml 文件中定义的片段吗?
是否可以使用FragmentTransaction 和remove() 方法来删除layout.xml 中定义的片段(使用fragment 标签)?
我没有使用支持库 v4 让它工作。在调用remove()之后提交FragmentTransaction后,片段将保持在原位。谁能告诉我这是设计使然、错误还是功能?
可以替换 lyaout.xml 中定义的片段,所以我觉得有点奇怪,不应该删除它?
Is is possible to use FragmentTransaction and the remove() method to get rid of fragments that are defined in the layout.xml (using the fragment tag) ?
I did not get this to work using the support libraries v4. The fragment stays in place after you commit the FragmentTransaction, after calling remove(). Can anyone tell me if this is by design, a bug or a feature?
It is possible to replace a fragment that is defined in the lyaout.xml, so I find it a bit strange that it should not be possible to remove it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Honeycomb 中可用的本机 API 的工作方式与支持库中的 API 相同,因此您无法删除已在布局 XML 文件中声明的 Fragment 实例。
使用 FragmentTransactions,您可以操作 ViewGroups(例如 LinearLayouts),它们充当容器来保存其他 Fragment 的布局。但是,当您在布局中声明
Fragment
时,它没有相同意义上的容器,因为它永久是 View 层次结构的一部分,因此您无法删除它。这是设计使然,为了支持诸如导航片段之类的东西,无论如何你都不会删除它们。 :)一件有趣的事情是我完全偶然发现的,那就是您可以将新的片段添加到在布局中使用标签声明的片段中;它充当其他 Fragment 的容器
The native APIs available starting in Honeycomb work the same as those in the support libarary, so you cannot remove an instance of a Fragment which has been declared in your layout XML file.
With FragmentTransactions you manipulate ViewGroups such as LinearLayouts that act as containers to hold the layout of other Fragments. However, when you declare a
Fragment
in your layout, it doesn't have a container in the same sense because it is permanently part of the View hierarchy, so you can't remove it. That is by design, to support things like navigation Fragments that you'd never remove anyways. :)One thing that's interesting, and I found it out totally by accident, is that you can add new Fragments into a Fragment that was declared with the tag in your layout; and it acts as a container for other Fragments
就像@david-c-sainte-claire 和@martín-marconcini 所说,您不能使用remove() 方法和FragmentTransaction 来删除XML 中定义的片段。这并不意味着你不走运。您始终可以使用 setVisibility() 方法。
Like @david-c-sainte-claire and @martín-marconcini said, you can't use remove() method and FragmentTransaction to remove the fragment that was defined in the XML. That doesn't mean you are out of luck. You can always use setVisibility() method.
这是设计使然(或者缺乏某个功能,如果你问我的话,这绝对不是一个功能:P)。所以只要你使用支持库,你就无法实现这一点。
This is by design (or a lack of a feature, not definitely a feature if you ask me :P). So as long as you are using the support libraries, you can't achieve this.