问题
From FragmentPagerAdapter
in case 0
I instantiated fragment A
this fragment I wanted to display display two fragments inside frag A
. The view is not displaying. MyFragmentPagerAdapter is called by the main class to populate the viewpager.
MyFragmentPagerAdapter extends FragmentPagerAdapter{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
final int MARGIN = 16;
leftFrag = new RoundedColourFragment(con.getResources().getColor(
R.color.android_green), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN);
rightFrag = new RoundedColourFragment(con.getResources().getColor(
R.color.honeycombish_blue), 2f, MARGIN / 2, MARGIN, MARGIN,
MARGIN);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragg, leftFrag, "left");
ft.add(R.id.fragg2, rightFrag, "right");
ft.commit();
return rootView;
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/fragg"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/fragg2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
回答1:
If you are going to nest fragments inside of fragments, you have to use getChildFragmentManager()
to set up the nested fragments, not getFragmentManager()
. getChildFragmentManager()
is part of API Level 17, and it is also in the Android Support package's backport of fragments.
来源:https://stackoverflow.com/questions/17970021/display-two-fragments-at-the-same-time