【问题标题】:Display Two Fragments at the same time同时显示两个片段
【发布时间】:2013-07-31 12:10:21
【问题描述】:

FragmentPagerAdaptercase 0 我实例化了fragment A 这个片段,我想在frag A 中显示两个片段。视图未显示。主类调用 MyFragmentPagerAdapter 来填充 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>

【问题讨论】:

    标签: java android android-fragments android-nested-fragment


    【解决方案1】:

    如果要将片段嵌套在片段中,则必须使用getChildFragmentManager() 来设置嵌套片段,而不是getFragmentManager()getChildFragmentManager() 是 API 级别 17 的一部分,它也在 Android 支持包的片段的反向移植中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2012-10-27
      相关资源
      最近更新 更多