Fragments inside a fragment Android application -
i'm trying put 2 fragments inside fragment. found code on internet but, far go, don't succeed put 2 fragments in 1 fragment. have seen tips dealing fragmentmanager , method getchildfragmentmanager() don't know how works 2 fragments.
for story, i'm using activity actionbar creates 3 fragments. in 1 of them, need handle graph , kind of menu change graph scale. in way, need 2 fragments in 1 fragment.
here code :
the fragment handles 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 draw 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 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 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, works @ begining when switch between fragments in actionbar after 3-4 moves, error :
android.view.inflateexception: binary xml file line #7: error inflating class fragment if has solution, awesome!
so first off change xml graph_fragment.xml 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 inflate them fragment use 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; }
Comments
Post a Comment