Just created a new project in the newest ADT release in Eclipse and found that it will setup up certain environments for you to get things started. I choose Tabs + Swipe.
It has this code I have question on:
public static class DummyFragment extends Fragment {
public DummyFragment () {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
Both tabs refer to this same fragment. All it does is switch the content on the TextView that has the tab position number on it (1,2, or 3).
-
The more advanced question first: I want two different Fragments that the tab switches to. In the example code, it points to the same fragment. Where does this change take place? and can I see brief code example?
-
Easier question: I have two pre-defined XML layouts I’d like to set each tab (or Fragment) with. Do I do this in the actual Fragment? And if so, where? setContentView does’t seem to be working in the onCreateView method?
Not really sure what this question is asking exactly, but if I understand correctly the
TabHost(or whatever you are using to manage theFragmenttabs) is instantiating multiple instances of theDummyFragmentand then attaching each to the screen when a tab is clicked. This is all done behind the scenes… all you need to worry about is implementing theFragmentand telling theTabHostwhen it should be instantiated/displayed.Fragments don’t have asetContentViewmethod. You should inflate yourFragment‘s layout from xml inonCreateView. For instance with,