简体   繁体   中英

Android ActionBar Tabs and Fragments

I am getting started with android and want to have an application that has several tabs. I want to use the actionbar with the SherlockActionBar library to support devices running pre honeycomb versions of 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.

It seems that when an button is clicked that is defined in a fragments layout the onclick is triggered in the parent activity. 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. 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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