My first ever Android code with Fragment
As an exercise, I am trying to rewrite the following google tutorial with Fragment class. The original tutorial implements tabs by using the old TabActivity class and TabHost/TabWidget annotation.
Tab Layout Google Tutorial
I have converted all Activity class with Fragment. I couldn't make my new code to work. I think I am stuck.I could not find any 'complete' Tab sample code using Fragment class.
Here are my questions 1. Should I define in the res/layout/main.xml or calling Actionbar.addTab(...) in my entry class, or both?
2. What would be complete res/layout/main.xml looks like? What would be the root e开发者_开发技巧lement (i.e. LinearLayout, FrameLayout...etc)? 3. Any additional info would be greatly appreciated.Check out this example from the compatibility library demos: FragmentTabs.java
and the corresponding layout: fragment_tabs.xml
Really, though, I wouldn't start with Tabs if you're trying out Fragments for the first time. Tabs in Android are a little bit of a mess. The above example (from Google itself) uses a hack just to get things working. Tabs just add a layer of unnecessary confusion when you're just learning.
Here's a more straightforward starting-out Fragments example/tutorial: http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
(Just make sure to replace things like getFragmentManager()
with getSupportFragmentManager()
if you're using the compatibility library.)
精彩评论