Display Two Fragments at the same time

落爺英雄遲暮 提交于 2019-12-12 20:27:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!