2012-11-22 77 views
0

我有一個framelayout(A)作爲我的baseActivity中的基礎視圖,然後添加一個附加視圖(B),其中可能包含一個SearchFragment。我想從這個佈局b卸下SearchFragment並將其添加到外佈局A.如何重新創建片段

private void reparentSearchFragment(ViewGroup view, FrameLayout container){ 
    View search = view.findViewById(R.id.search_fragment); 
     if(search != null && view instanceof ViewGroup){ 
      view.removeView(search); 
      container.addView(search); 
    } 
} 

這似乎是失敗的,日誌Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我覺得這很奇怪,因爲我刪除的看法,正如你在代碼片段中看到的那樣。有任何想法嗎?謝謝:)

+1

你怎麼片段添加到B-佈局?從Java或XML? –

+0

片段以xml中的B佈局列出 – serenskye

回答

1

將代碼片段添加到B佈局中。

而且當你需要把它放在一個,你將需要刪除的片段,然後重新添加這樣的:

SearchFragment s = ...; 
FragmentTransaction t = getSupportFragmentManager().beginTransaction(); 
t.remove(s); 
t.add(R.layout.A_ID, s); 
t.commit(); 
1

嘗試使用動態片段(不是從xml佈局文件添加),然後使用FragmentTransaction API。

相關問題