Android FragmentTabHost不';不显示选项卡内容
我有一个应用程序,它包含一个用于导航的抽屉菜单和一个片段活动,其中根据抽屉选择显示一个片段。现在我有2个片段,一个简单的片段(SettingsFragment)和一个包含FragmentTabHost的片段(SearchFragment)。打开应用程序时,将显示FragmentTabHost的。问题是,如果我在片段之间切换-转到设置片段,然后返回SearchFragment,那么tabhost的内容将不再显示(第一个选项卡中包含的片段的onCreateView不会被调用),除非我在选项卡之间切换 因此,我在活动中使用replace在片段之间进行更改:Android FragmentTabHost不';不显示选项卡内容,android,android-fragments,navigation-drawer,fragment-tab-host,Android,Android Fragments,Navigation Drawer,Fragment Tab Host,我有一个应用程序,它包含一个用于导航的抽屉菜单和一个片段活动,其中根据抽屉选择显示一个片段。现在我有2个片段,一个简单的片段(SettingsFragment)和一个包含FragmentTabHost的片段(SearchFragment)。打开应用程序时,将显示FragmentTabHost的。问题是,如果我在片段之间切换-转到设置片段,然后返回SearchFragment,那么tabhost的内容将不再显示(第一个选项卡中包含的片段的onCreateView不会被调用),除非我在选项卡之间切换
public void replaceRightFragment(int fragmentId) {
Fragment newFragment = null;
switch (fragmentId) {
case FRAGMENT_SEARCH_PROPERTY:
newFragment = getSupportFragmentManager().findFragmentByTag(SearchPropertyFragment.class.getSimpleName());
if (newFragment == null) {
newFragment = new SearchPropertyFragment();
}
break;
case FRAGMENT_SETTINGS:
newFragment = getSupportFragmentManager().findFragmentByTag(SettingsFragment.class.getSimpleName());
if (newFragment == null) {
newFragment = new SettingsFragment();
}
break;
}
if (newFragment != null) {
if (!newFragment.isVisible()) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
fragmentTransaction.replace(R.id.main_view, newFragment, newFragment.getClass().getSimpleName());
// fragmentTransaction.remove(mainFragment);
// fragmentTransaction.add(R.id.main_view, newFragment, newFragment.getClass().getSimpleName());
// fragmentTransaction.addToBackStack(null);
// Commit the transaction
fragmentTransaction.commitAllowingStateLoss();
}
hideSlideMenu();
hideKeyboard();
}
}
我的选项卡主机布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/header_bar"/>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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"
android:layout_weight="0"
android:orientation="horizontal"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
有什么提示吗
不需要TabWidget,FragmentTabHost将自动添加它。
如果您希望选项卡位于顶部,请将此按钮置于realtabcontent上方
但是你可以像我一样定制它
<?xml version="1.0" encoding="utf-8"?>
在另一个
片段中使用FragmentTabHost
(其中选项卡是片段)时,需要将getChildFragmentManager()
传递给活动的setup()
方法,而不是FragmentManager
。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@android:id/tabhost" />
<FrameLayout
android:id="@+id/homeContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@android:id/tabhost"
android:layout_marginTop="@dimen/button_height"
android:background="#ffffff"
android:visibility="gone" >
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:orientation="horizontal" >
<ImageView
android:id="@+id/btn_homeScreen_tab"
android:layout_width="@dimen/button_height"
android:layout_height="@dimen/button_height"
android:layout_alignParentLeft="true"
android:src="@drawable/grange_logo" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/button_height"
android:layout_marginRight="@dimen/button_height" />
<ImageView
android:id="@+id/ph_lable_title"
android:layout_width="@dimen/button_height"
android:layout_height="@dimen/button_height"
android:layout_alignParentRight="true"
android:src="@drawable/contact_1" />
</RelativeLayout>