简体   繁体   English

使用ActionBar选项卡和嵌套片段进行导航

[英]Navigation with ActionBar Tabs & nested fragments

If you are using an Actionbar with Tabs for navigation, should we be using fragments only? 如果您要使用带有“标签”的操作栏进行导航,是否应该仅使用片段? When I'm looking at the developer site, I only see examples where they are switching Fragments, not Activities. 当我查看开发人员站点时,仅看到示例在其中切换片段而不是活动的示例。 The guidelines tell us to use a ViewPager when using tabs, so you should use Fragments to make that work. 该准则告诉我们在使用选项卡时要使用ViewPager,因此您应该使用Fragments来使它起作用。

The problem is that these fragments will contain quite a lot. 问题在于这些片段将包含很多内容。 They should have other fragments as well. 他们也应该有其他片段。 Nested fragments. 嵌套的片段。 These are supported when using the Support Library (or targetting api level 17). 在使用支持库(或目标api级别17)时支持这些功能。

My main concern is how the communication will run between the fragments at the bottom of the hierarchy. 我主要关心的是通信将如何在层次结构底部的各个片段之间进行。 Will it all run through that one Activity? 会全部完成该活动吗?

You won't have problems. 你不会有问题的。 Just treat your fragments as something independent that will be placed in a container. 只需将片段视为独立的东西,然后将其放入容器即可。 That fragment can have more fragments and so on. 该片段可以有更多片段,依此类推。

Some people are developing fragment based applications with a single Activity and sometimes makes sense, however I don't like that approach much. 有些人正在使用一个Activity开发基于片段的应用程序,有时这是有道理的,但是我不太喜欢这种方法。

That said, just use all the fragments you need. 也就是说,只需使用您需要的所有片段。 If you have complex data to pass between fragments you could use the activity to host it and access it in your fragments. 如果您要在片段之间传递复杂的数据,则可以使用该活动来托管它并在片段中对其进行访问。

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

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