سؤال

I am trying to build a three pane layout (three ListFragment). I am able to create it inside an activity by following layout

<?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>

But I am using nested fragment and in nested fragment, fragments have to be added dynamically into FrameLayout and that's why I wrote these code:

<?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>

But it is not working. How can I do this?

Edit:

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);
        }
    }

}

Three fragments are like:

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

No need to add the fragments in onActivityCreated. Just change the android:orientation="vertical" to horizontal. Everything is okay except it!

نصائح أخرى

move the following to 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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top