在相同的布局使用两次片段 [英] Use fragment twice in same layout

查看:116
本文介绍了在相同的布局使用两次片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在一个布局重用片段两次吗?我有两个标签,标签A和标签乙..每个选项卡显示由左侧项目和右侧的所选项目的细节(B型或)列表的视图的布局。 详细信息视图(右侧窗格)是两种类型的列表一样的,所以我想把它留而列表取决于所选什么标签交换出去。

Is it possible to reuse a fragment twice in one layout? I have a layout of two tabs, Tab A and Tab B.. each tab displays a view that consists of a list (of type A or B) of items on the left and the details of the selected item on the right. The "detail view" (on the right pane) is the same for both types of lists, so I want to have it remain while the list is swapped out depending on what tab is selected.

我曾尝试,包括在布局XML为两个标签的细节片段,但获得有关重复运行时错误。于是我试着在运行时有FragmentTransaction动态地添加他们却得到了一个类似的错误(片段已添加)。

I have tried to include the details fragment in layout XML for both tabs but get a runtime error about duplicates. So then I tried to add them dynamically at runtime with a FragmentTransaction but get a similar error (fragment has already been added).

是我想做的事情可能吗?

Is what i want to do possible?

推荐答案

您应该添加片段动态设定不同的标记每个片段,然后用 getFragmentManager()findFragmentByTag(TAG); 在每个选项卡视图中可以添加的FrameLayout ,然后用 FragmentTransaction.replace 功能。

You should add the fragments dynamically setting different TAGs for each fragment and then finding them with getFragmentManager().findFragmentByTag("TAG"); In each TAB view you can add a FrameLayout and then use FragmentTransaction.replace function.

我有一个类似的问题(添加相同片段多次在 ViewFlipper ),并利用它解决不同的标签。

I had a similar problem (adding the same fragment many times in a ViewFlipper) and solved it by using different tags.

这篇关于在相同的布局使用两次片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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