4

I'm trying to put 2 fragments inside a fragment. I found some code on internet but, as far as I could go, I don't succeed to put 2 fragments in 1 fragment. I have seen tips dealing with FragmentManager and especially the method getChildFragmentManager() but I don't know how it works with 2 fragments.

For the story, I'm using an activity with ActionBar which creates 3 fragments. In one of them, I need to handle a graph and a kind of menu to change the graph scale. In this way, I need 2 fragments in one fragment.

Here is the code :

The fragment which handles the others:

public class GraphDisplayFragment extends Fragment {

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {

  View myFragmentView = inflater.inflate(R.layout.graph_fragment, container, false);

  return myFragmentView;

 }
}

The code to draw the graph:

public class GraphFragment extends Fragment {
private static final int SERIES_NR = 1;

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {

  GraphicalView myFragmentView = ChartFactory.getTimeChartView(this.getActivity(), getDateDemoDataset(), getDemoRenderer(),null);
  return myFragmentView;

 }

//some functions to set graph propreties
}

The XML files :

graph_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >   
<fragment
    android:id="@+id/graph_fragment"
    android:name="com.test.GraphFragment"
    android:layout_width="match_parent"
    android:layout_height="259dp" >

</fragment>
<fragment
    android:name="com.test.GraphDetailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/graph_detail_fragment">
</fragment>
</LinearLayout>

graph_detail.xml with a test implementation

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="211dp"
    android:layout_height="wrap_content"
    android:text="TextView" />
 </LinearLayout>

The weird thing is, it works at the begining when I switch between fragments in the ActionBar but after 3-4 moves, I get this error :

android.view.InflateException: Binary XML file line #7: Error inflating class fragment

If anyone has the solution, it would be awesome!

Blo
  • 11,903
  • 5
  • 45
  • 99
Arthur Allnc
  • 41
  • 1
  • 4

3 Answers3

2

So first off change your xml for graph_fragment.xml to this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >   
   <FrameLayout
          android:id="@+id/graph_fragment_holder"
          android:layout_width="match_parent"
          android:layout_height="259dp" > 

   </FrameLayout>
   <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/graph_detail_fragment_holder">
   </FrameLayout>
</LinearLayout>

Then in code to inflate them from the fragment use something like this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.graph_fragment,
            container, false);
    FirstChildFragment frag1 = (FirstChildFragment) getChildFragmentManager()
            .findFragmentById(R.id.first_frag_layout);
    SecondChildFragment frag1 = (SecondChildFragment) getChildFragmentManager()
            .findFragmentById(R.id.second_frag_layout);
    if (null == frag1) {
        FirstChildFragment = new frag1();
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();

        transaction.add(R.id.graph_fragment_holder, frag1)
                .addToBackStack(null).commit();
    }

     if (null == frag2) {
        SecondChildFragment = new frag2();
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();

        transaction.add(R.id.graph_detail_fragment_holder, frag2)
                .addToBackStack(null).commit();
    }

    return rootView;
}
  • if your ide says it can't find the method getChildFragmentManager don't worry it'll still compile and run smoothly. – SemaphoreMetaphor Jul 18 '13 at 14:57
  • Thanks for your answer. I tried it but I don't know what you mean by `FirstChildFragment = new frag1();` so I tried with `FirstChildFragment frag = frag1;` and I get an error when I switch between fragments on the ActionBar : `java.lang.NullPointerException at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)` – Arthur Allnc Jul 19 '13 at 08:00
  • That's where you put the names of your fragments. You also need to fill in the correct layout names for them. Hope that clears it up – SemaphoreMetaphor Jul 19 '13 at 15:01
  • So change FirstChildFragment to the name of your first fragment and change this R.id.first_frag_layout to its approprite layout name and do the same for the second child fragment – SemaphoreMetaphor Jul 19 '13 at 15:18
  • Creating the `Fragment` manually and adding it with a `FragmentTransaction` did the trick! Thx – Raphael Royer-Rivard Jan 19 '15 at 19:13
1

MyFragment myFragment = (MyFragment)getChildFragmentManager().findFragmentById(R.id.my_fragment);

Sergio
  • 34
  • 3
0

You can try to use the method *findFragmentById(fragment_id)* from SupportFragmentManager to get the instance of fragment from your .xml file and insert your fragment there.

Something like:

Fragment myFragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId);

I hope to help you.

Best regards. Adriano