假如tabHost中的所有标签页对应的view有对应的layout.xml配置文件,那么我们可以用tabhost.addTab(tabhost.newTabSpec(“xxx”).setIndicator(“xxx”).setContent(intent));来完成,那么假如View是要自己动态创建的,怎么办?
假如View是要自己动态创建的,那么需要放置TabHost下的FrameLayout中,不然的话是显示不了的,而且程序还会抛出找不到View的错误。
在把View动态添加到FrameLayout之后呢,我们需要在ViewPager适配器添加一些语句,也就是在重写inistrntiateItem方法的时候,也就是在ViewPager添加View的时候,需要把frameLayout对应的View删除掉,不然会爆出:The specified child already has a parent. You mustcall removeView() on the child's parent first.原因很简单:frameLayout、viewPager同时是同一个view的parent的,这样是不允许的,所以要先移除frameLayout这个parent。
public Object instantiateItem(View arg0, int arg1) {
frameLayout.removeView(mListViews.get(arg1));
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}