Android animated tab content switching
I'm trying to animate view changes when I click on tabs in my android app with ViewFlipper, but the problem is that views that were added to TabSpec with setContent can no longer be added to ViewFlipper or this exception is thrown:
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.
How I'm adding tabs
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)
And later
for (View tabView : tabViews) {
flipper.addView(tabView);
}
I'm aiming for 开发者_StackOverflow社区an effect like this http://www.youtube.com/watch?v=SZTiJmclaRc only when you click the tabs. I'm planning to use onTabChanged event to file
ViewFlipper.setDisplayChild(flipperViewIndexDerivedFromTabIndex)
Thanks for any help.
Got this to work without any ugly ArrayList hacking. The key was changing View type for tabcontent in my layout xml from FrameLayout to ViewFlipper.
I'm now able to pass my tab content as TabContentFactory object as such
spec.setContent(contentFactory);
and access ViewFlipper in onTabChanged method by
ViewFlipper flipper = (ViewFlipper) getTabHost().getTabContentView();
after that it's just implementing logic to choose view to flip to. Revelation came from browsing other android tagged questions on SO!
精彩评论