これが私の解決策です。これで、フラグメントからgetChildFragmentManagerを使用できます。これは新しいSupportLibraryに含まれています。私の本番ソリューションでは、最初にフラグメントが存在するかどうかをテストし、必要な場合にのみ追加します。
public class TestActivity extends SherlockFragmentActivity {
private ViewPager mPager;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.pager);
mPager = (ViewPager) findViewById(R.id.viewPager);
mPager.setAdapter(new TestAdapter(getSupportFragmentManager()));
}
public class TestAdapter extends FragmentStatePagerAdapter {
public TestAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(position);
}
@Override
public int getCount() {
return 4;
}
}
public static class TestFragment extends SherlockFragment {
private static final int ID = 9129345;
private static final String ARG_INDEX = "TestFragment.pageindex";
public static TestFragment newInstance(int page) {
TestFragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putInt(ARG_INDEX, page);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.root_container, null);
int pageId = getArguments().getInt(ARG_INDEX);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.live_radio, TextFragment.newInstance("PAGE", pageId + 1));
transaction.add(R.id.breaking_news, TextFragment.newInstance("TEXT", 1));
transaction.add(R.id.main_container, TextFragment.newInstance("TEXT", 2));
transaction.commit();
return view;
}
}
public static class TextFragment extends SherlockFragment {
private static final String ARG_INDEX = "TextFragment.index";
private static final String ARG_TEXT = "TextFragment.text";
public static TextFragment newInstance(String text, int index) {
TextFragment fragment = new TextFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
args.putInt(ARG_INDEX, index);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textView.setText(getArguments().getString(ARG_TEXT) + " " + getArguments().getInt(ARG_INDEX));
return textView;
}
}
}
ビューページャーを使用したメインレイアウト
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/player"
android:layout_alignParentTop="true" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
フラグメントレイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/live_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/breaking_news"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>