0

我有一個應用程序女巫包含一個抽屜菜單導航和片段活動,其中根據抽屜選擇顯示一個片段。現在我有2個片段,一個簡單的(SettingsFragment)和一個包含FragmentTabHost(SearchFragment)的片段。帶有FragmentTabHost的應用程序打開時顯示。問題是,如果我在片段之間切換 - 轉到設置片段,然後返回到SearchFragment,那麼tabhost的內容不再顯示(包含在第一個選項卡中的片段的onCreateView不會被調用),除非我在選項卡之間切換。FragmentTabHost不顯示標籤內容

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(); 
     } 

    } 

我tabhost佈局:

<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> 

...和tabhost設置在片段:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_search_property, 
      container, false); 

    mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost); 
    mTabHost.setup(getActivity(), 
      getActivity().getSupportFragmentManager(), R.id.realtabcontent); 

    mTabHost.addTab(
      mTabHost.newTabSpec(SELL_FRAGMENT_TAG).setIndicator(
        getString(R.string.search_property_sell_tab_title)), 
      SearchPropertySellFragmentTab.class, null); 
    mTabHost.addTab(
      mTabHost.newTabSpec(RENT_FRAGMENT_TAG).setIndicator(
        getString(R.string.search_property_rent_tab_title)), 
      SearchPropertyRentFragmentTab.class, null); 
    mTabHost.addTab(
      mTabHost.newTabSpec(ADD_ADVERT_FRAGMENT_TAG).setIndicator(
        getString(R.string.search_property_add_tab_title)), 
      SearchPropertyAddAdvertFragmentTab.class, null); 

    ImageButton mDrawerButton = (ImageButton) view.findViewById(R.id.drawer_button); 
    mDrawerButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ((DrawerMenuController) getActivity()).onDrawerButtonPressed(); 
//   if (mDrawerLayout.isDrawerOpen(mDrawerList)) { 
//    mDrawerLayout.closeDrawer(mDrawerList); 
//   } else { 
//    mDrawerLayout.openDrawer(mDrawerList); 
//   } 
     } 
    }); 
    mTabHost.onTabChanged(SELL_FRAGMENT_TAG); 
    mTabHost.setOnTabChangedListener(this); 

    return view; 
} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    mTabHost.removeAllViews(); 
    mTabHost = null; 
} 

任何

所以我用我的活動取代片段之間改變提示?

+0

當你使用'FragmentTabHost'(其中標籤是片段)在另一個'Fragment'需要將'getChildFragmentManager()'傳遞給'setup()'方法,而不是活動的'FragmentManager'。 – Luksprog

回答

1

不需要TabWidget,FragmentTabHost會自動添加它。 如果您希望標籤位於頂部,請將此標籤置於realtab內容之上!

,但你可以自定義它像這樣像我一樣...

<?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>