Reputation: 788
I used the Eclipse project wizard to create a project with an ActionBar and tabs. The wizard creates a dummy fragment for each tab with dummy text just showing the tab number. The app works without problems.
The code looks like this:
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, show the tab contents in the container
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
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;
}
}
For the purpose of my App I would like to show one fragment when the device is in portrait mode and two fragments when the device is in landscape mode. I know about the Shakespeare sample but in this sample it is an activity the holds the one or two fragments.
The Shakespeare sample uses two different layouts. For portrait mode (in "layout\fragment_layout_support.xml"):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class=".FragmentLayoutSupport$TitlesFragment"
android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
and for landscape mode (in "layout-land\fragment_layout_support.xml"):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class=".FragmentLayoutSupport$TitlesFragment"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
</LinearLayout>
The Shakespeare sample loads the layout like this:
public class FragmentLayoutSupport extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout_support);
}
...
How do I do the same for my App where I cannot load the layout in a FragmentActivity?
Thanks.
Upvotes: 3
Views: 517
Reputation: 546
Perhaps you're stuck because you don't know that ActionBarActivity inherits FragmentActivity ?
So simply replace
extends FragmentActivity
with
extends ActionBarActivity
and the correct imports (see ActionBar API Guide). You will then be able to implement the same behaviour as in the Shakespeare sample while keeping your ActionBar/Tabs. Just extend the FragmentLayoutSupport instead of ActionBarActivity in your previously working activity.
Upvotes: 1