如何使用三个平移布局在嵌套片段中添加动态片段

时间:2013-08-11 21:28:10

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

我正在尝试构建一个三窗格布局(三个ListFragment)。我可以通过以下布局

在活动中创建它
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Fragment android:name="com.example.android.fragments.HeadlinesFragment"
        android:id="@+id/category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Fragment android:name="com.example.android.fragments.HeadlinesFragment2"
        android:id="@+id/sub_category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Fragment android:name="com.example.android.fragments.HeadlinesFragment3"
        android:id="@+id/sub_sub_category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

但我使用nested fragment并且在嵌套片段中,必须将片段动态添加到FrameLayout中,这就是我编写这些代码的原因:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/sub_category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/sub_sub_category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

但它不起作用。我怎么能这样做?

编辑:

public class CompetitiveProgramming extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.competitive_programming_exercise, container, false);
        Fragment a = new HeadlinesFragment();
        Fragment b = new HeadlinesFragment2();
        Fragment c = new HeadlinesFragment3();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.category_fragment, a );
        transaction.add(R.id.sub_category_fragment, b );
        transaction.add(R.id.sub_sub_category_fragment, c );
        transaction.commit();
        return rootView;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}

三个片段如:

public class HeadlinesFragment extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      TextView textView=new TextView(getActivity());
      textView.setText("Hello I am fragment C");
      return textView;
   }
}

2 个答案:

答案 0 :(得分:2)

将以下内容移至onActivityCreated

    Fragment a = new HeadlinesFragment();
    Fragment b = new HeadlinesFragment2();
    Fragment c = new HeadlinesFragment3();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.category_fragment, a );
    transaction.add(R.id.sub_category_fragment, b );
    transaction.add(R.id.sub_sub_category_fragment, c );
    transaction.commit();

答案 1 :(得分:2)

无需在fragment中添加onActivityCreated。只需将android:orientation="vertical"更改为horizontal即可。一切都没关系,除了它!