2011-03-03 92 views
0

我試圖使用ViewFlipper在我的android應用程序中單擊選項卡時激活視圖更改,但問題是添加到帶有setContent的TabSpec的視圖不能再添加到ViewFlipper或此拋出異常:Android動畫標籤內容切換

03-03 19:12:19.082: ERROR/AndroidRuntime(229): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

如何我添加標籤

private ArrayList<View> tabViews = new ArrayList<View>(); 
private ViewFlipper flipper; 
.... 
View tabView = contentFactory.createTabContent("tab_" + labelId); 
tabViews.add(tabView); 

mTabHost.getTabContentView().addView(tabView); 

spec.setIndicator(tabIndicator); 
spec.setContent(tabView.getId()); 
mTabHost.addTab(spec) 

後來

for (View tabView : tabViews) { 
    flipper.addView(tabView); 
} 

僅當您單擊選項卡時,我纔會瞄準這種效果,如http://www.youtube.com/watch?v=SZTiJmclaRc。我打算使用onTabChanged事件來存檔

ViewFlipper.setDisplayChild(flipperViewIndexDerivedFromTabIndex) 

感謝您的任何幫助。

回答

1

得到這個工作沒有任何醜陋ArrayList黑客入侵。關鍵在於我的佈局xml中的tabcontent的View類型從FrameLayout更改爲ViewFlipper。

我現在可以通過

ViewFlipper flipper = (ViewFlipper) getTabHost().getTabContentView(); 

之後,它只是實現邏輯選擇視圖通過我的標籤內容作爲TabContentFactory對象這樣

spec.setContent(contentFactory); 

和訪問ViewFlipper在onTabChanged方法翻轉到。啓示錄來自於瀏覽其他Android標記的問題!

+0

有了您的解決方案,我該如何實現交換機?我改變了我的XML從framelayout查看鰭狀肢,並設置我的標籤更改偵聽器。在onCreate這是我如何添加我的標籤:spec = tabHost.newTabSpec(「alerts」)。setIndicator(tabTitle,res.getDrawable(R.drawable.alertsbtn))。setContent(intent); 我還在做什麼onTabChanged?通常我會定義視圖來在xml中翻轉。 – spentak 2011-07-14 16:06:50