我試圖把2片段放入片段中。我在互聯網上發現了一些代碼,但是,就我所能去的,我沒有成功將2個片段放入1個片段中。 我已經看到了處理FragmentManager的提示,特別是getChildFragmentManager()方法,但我不知道它如何處理2個片段。片段內的一個片段Android應用程序
對於這個故事,我正在使用ActionBar來創建3個片段。其中之一,我需要處理一個圖形和一種菜單來改變圖形比例。這樣,我需要在一個片段中有2個片段。
下面是代碼:
它處理的其他片段:
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;
}
}
的代碼繪製圖形:
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
}
的XML文件:
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與測試實施
<?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>
奇怪的是,它工作在開始時,當我在動作條,但後3-4移動片段之間切換,我得到這個錯誤:
android.view.InflateException: Binary XML file line #7: Error inflating class fragment
如果有人有解決方案,這將是太棒了!
請參考[中的片段片段]的http://stackoverflow.com/a/11020531/1219456 –
可能重複(http://stackoverflow.com/questions/6847460/fragments-within-fragments) – rds