简体   繁体   English

Android ActionBar选项卡和片段

[英]Android ActionBar Tabs and Fragments

I am getting started with android and want to have an application that has several tabs. 我正在开始使用android,并希望有一个带有多个标签的应用程序。 I want to use the actionbar with the SherlockActionBar library to support devices running pre honeycomb versions of android. 我想将动作条与SherlockActionBar库一起使用,以支持运行Android之前的蜂窝版本的设备。 I have seen so many examples of creating and using tabs that I have no idea now what is the best or preferred method of doing so. 我已经看到了许多创建和使用选项卡的示例,现在我不知道哪种方法是最好的或首选的方法。

I have read that the best way is to have a fragment for each tab and have one activity which controls the switching of fragments in an out as well as coordinating communication between fragments. 我已经读到,最好的方法是为每个选项卡都有一个片段,并有一个活动来控制片段的切入和协调片段之间的通信。 If this is the proper way of handling tabs is it best to have each fragments layout defined in its own layout.xml. 如果这是处理选项卡的正确方法,则最好在其自己的layout.xml中定义每个片段布局。

It seems that when an button is clicked that is defined in a fragments layout the onclick is triggered in the parent activity. 似乎当单击片段布局中定义的按钮时,onclick在父活动中触发。 If the best way to handle that to do the processing in the parent activity or to pass the event on to the correct fragment for processing. 处理该问题的最佳方法是在父活动中进行处理或将事件传递到正确的片段进行处理。

In summary I want to know what the best practice is for creating an app with actionbar tabs, where the code should be and how the layouts should be separated. 总而言之,我想知道使用动作栏标签创建应用的最佳实践是什么,代码应该在哪里,以及布局应该如何分离。

Thank You, Nathan 谢谢你,内森

You are in the right direction from what I can read. 根据我的理解,您的方向正确。 Google has a lot of the best material on this subject. Google在这方面有很多最好的资料。 Also look at Googles apps for reference, the Play app features simple tab navigation and defines how we should and should not try to make tabs. 另外,请参考Google应用程序,Play应用程序具有简单的标签导航功能,并定义了我们应该和不应该尝试制作标签的方式。 One very important lesson is that we should only have 1 fragment in each tab and we shouldn't try to make back navigation inside tabs (though it is possible by hacking a little). 一个非常重要的教训是,每个选项卡中应该只有一个片段,并且我们不应该尝试在选项卡中进行向后导航(尽管可以通过少量修改来实现)。

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

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