1

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
  • show us the code that you are using to dynamically allocate the fragments. Commented Aug 11, 2013 at 21:31
  • @Pork I have added. Take a look at the update. Commented Aug 11, 2013 at 21:34

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

It is not working! It is showing a white screen like before. If I make layout_width = "200dp" instead of 0dp, it is showing. But I don't need that :(
even with layout_width at 200dp, is it not resulting in 3 even sized columns?
No, I set layout_width=200dp for only first fragment and only this one is shown
2

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

1 Comment

Hi Kaidul,can you please look in my issue?? Its very similar.. stackoverflow.com/questions/32240138/…. For me the child framnet is not getting inflated from the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.