ViewPager 和片段 —设计嵌套片段或..?
文档说你不应该嵌套片段,因为它们被设计成以另一种方式工作。但!
范围
但是,如果我使用 ViewPager 并且它的片段(可见屏幕)应该由其他片段构建,该怎么办?假设我们有一个消息应用程序。在主屏幕上有一个 ViewPager,其中包含名为“最后消息”(MessagesFragment) 的片段,在子主屏幕(比如说讨论室或聊天室)上,我们也有一个 ViewPager,但消息屏幕由 MessagesFragment、NewMessageBarFragment 和例如带有其他信息的标题片段组成。
问题
一可以说我们不应该为此使用 ViewPager,我们可以使用 ViewFlow 开源库,但相信我,在此类活动中举行的代码即使有碎片也是一团糟。还有一种方法可以填充额外的布局,而不是 MessagesFragment 中的 NewMessageBar 和 HeaderInfo 的片段 - 但对我来说它是双重丑陋的。
问题
在这种特殊情况下可以使用嵌套片段吗?你会推荐什么?
更新
有时它确实是“必须具备”的功能。但!在大多数情况下,我建议遵循“响应式设计”方式: http:// /www.slideshare.net/kirillcool/responsive-mobile-design-in-practice
Docs say you shouldn't nest fragments 'cause they are designed to work another way. But!
Scope
But what if I use ViewPager and it's fragment (visible screen) should be constructed from other fragments. Let's say we have a messenger app. On the main screen there's a ViewPager with fragment called "Last messages" (MessagesFragment) and on the submain screen (let's say Discussion or Chat room) we also have a ViewPager but Messages screen consists of MessagesFragment, NewMessageBarFragment and e.g. header fragment with other info.
Problem
One can say we shouldn't use ViewPager for that and we can use ViewFlow open source library but believe me the code, holded in such activity is a mess even with fragments. Also there's an approach to fill extra layouts, not fragments for NewMessageBar and HeaderInfo within MessagesFragment — but it's double ugly as for me.
Question
Is it OK to use nested fragments in this particular situation? What would you recommend?
Upadate
Sometimes it's really 'must have' feature. But! In most cases I would recommend follow 'responsive design' way: http://www.slideshare.net/kirillcool/responsive-mobile-design-in-practice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
快速更新:
嵌套
Fragment
已在 Android 中正式引入API 版本 17。A quick update:
Nested
Fragment
s were officially introduced in Android API version 17.不要那样做。
绝对不是。
用简单的布局替换“外部”片段。然后使用其他一些本身不需要片段的
PagerAdapter
实现(如FragmentPagerAdapter
接口)。您可能需要从FragmentPagerAdapter
中提取一些代码以确保这一切正常工作。或者,将“MessagesFragment、NewMessageBarFragment 以及例如带有其他信息的标题片段”组合到单个片段中,以便在您希望具有此寻呼机行为的屏幕尺寸上使用。这可能会导致一定量的代码重复,以便针对较小的屏幕尺寸单独分解 UI 的该部分。
Don't do that.
Absolutely not.
Replace your "outer" fragments with simple layouts. Then use some other
PagerAdapter
implementation that does not itself require a fragment (as does theFragmentPagerAdapter
interface). You may need to pinch some code fromFragmentPagerAdapter
to make sure this all works well.Or, combine "MessagesFragment, NewMessageBarFragment and e.g. header fragment with other info" into a single fragment for use on screen sizes where you want to have this pager behavior. This may result in some amount of code duplication, to have that portion of the UI broken out separately for smaller screen sizes.
这是我的解决方案。现在您可以从片段中使用 getChildFragmentManager 。它包含在新的支持库中。在我的生产解决方案中,我首先测试片段是否存在,并仅在必要时添加它们。
主布局与 viewpager
片段布局
Here is my solution. Now you can user getChildFragmentManager from fragment. It is included in new SupportLibrary. In my production solution I first test if fragments exist and add them only when necessary.
Main Layout with viewpager
Fragment layout