简体   繁体   中英

Fragments inside a fragment Android application

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!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM