viewpager页面里,2个靠近的页面都使用了tabhost,可以正常运行,但是后初始化的那个界面的tabhost会部署到
先初始化完成的那个页面里。
我是这么完成tabhost的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_frg5, container, false);
tabHost5 = (TabHost) view.findViewById(R.id.tabHost1);
tabHost5.setup();
tabHost5.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tab) {
System.out.println("current tabid=" + tab);
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (TextUtils.equals("3", tab)) {
//add/replace fragment first
frag5 = new frg5_frg1();
System.out.println("load f3");
} else if (TextUtils.equals("4", tab)) {
//add/replace fragment second
frag5 = new frg5_frg2();
System.out.println("load f4");
} else if (TextUtils.equals("5", tab)) {
//add/replace fragment second
frag5 = new frg5_frg3();
System.out.println("load f5");
}
ft.replace(android.R.id.tabcontent, frag5, "frag5");
ft.commit();
}
});
tabHost5.addTab(tabHost5.newTabSpec("3").setIndicator("环境设置")
.setContent(new DummyTabFactory(getActivity())));
tabHost5.addTab(tabHost5.newTabSpec("4").setIndicator("安全设置")//setIndicator 设置标签样式
.setContent(new DummyTabFactory(getActivity())));//setContent 点击标签后触发
tabHost5.addTab(tabHost5.newTabSpec("5").setIndicator("密码设置")//setIndicator 设置标签样式
.setContent(new DummyTabFactory(getActivity()))); //setContent 点击标签后触发
return view;
}
private static class DummyTabFactory implements TabHost.TabContentFactory {
private Context context;
private DummyTabFactory(Context ctx) {
this.context = ctx;
}
@Override
public View createTabContent(String tag1) {//创建宽高均为0的view
View v = new ImageView(context);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
}
布局页面
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabHost1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
</TabHost>
2个tabhost都是这么完成的,但是因为必须用@android:id/tabcontent。所以2个界面的布局就变得重复了,
后加载的那个tabhost会把前面的盖掉。我想过把viewpager的预加载关掉,那样就不会提前初始化下一个tabhost,但学的太少,去掉预加载老是出问题。
现在为了实现功能,把一个tabhost改成了取消滑动的viewpager,虽然看着像,但终究不是tabhost。
tabhost的实现方法问题吗?