2013-07-18 48 views
4

我試圖把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 

如果有人有解決方案,這將是太棒了!

+0

請參考[中的片段片段]的http://stackoverflow.com/a/11020531/1219456 –

+0

可能重複(http://stackoverflow.com/questions/6847460/fragments-within-fragments) – rds

回答

0

您可以嘗試使用SupportFragmentManager中的方法* findFragmentById(fragment_id)*從.xml文件中獲取片段實例並在其中插入片段。

喜歡的東西:

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

我希望能幫助你。

此致敬禮。 阿德里亞諾

2

所以第一關更改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" > 
    <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> 

然後在代碼從片段使用這樣的事情

@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; 
} 
+0

如果您的IDE說它找不到方法getChildFragmentManager不用擔心它仍然會編譯和順利運行。 – SemaphoreMetaphor

+0

感謝您的回答。我試過了,但是我不知道'FirstChildFragment = new frag1();'的意思,所以我嘗試了'FirstChildFragment frag = frag1;',並且在ActionBar之間切換片段時出現錯誤:'java。郎。NullPointerException在android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)' –

+0

這就是你放置碎片名稱的地方。您還需要爲它們填寫正確的佈局名稱。希望清除它 – SemaphoreMetaphor

1

MyFragment myFragment =(MyFragment他們膨脹。)getChildFragmentManager()findFragmentById(R.id.my_fragment);