2014-06-23 37 views
0

我正在按照教程進行片段內兩個子片段的製表導航。但在我的情況下,只顯示了兩個tabindicator,但沒有附加子片段。FragmentTabHost和ChildFragment - ChildFragment未顯示

父片段

public class WifisFragment extends SherlockFragment { 

    private static final String TAG = WifisFragment.class.getName(); 
    private static final String TAG1 = TAG + 1; 
    private static final String TAG2 = TAG + 2; 

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

     FragmentTabHost tabHost = (FragmentTabHost) root 
       .findViewById(android.R.id.tabhost); 

     tabHost.setup(getSherlockActivity(), getChildFragmentManager(), 
       android.R.id.tabcontent); 

     Bundle arg = new Bundle(); 
     arg.putInt(ChildFragment.POSITION_KEY, 1); 
     TabSpec tabSpec = tabHost.newTabSpec(TAG1).setIndicator("First"); 
     tabHost.addTab(tabSpec, ChildFragment.class, arg); 

     arg = new Bundle(); 
     arg.putInt(ChildFragment.POSITION_KEY, 2); 
     tabSpec = tabHost.newTabSpec(TAG2).setIndicator("Second"); 
     tabHost.addTab(tabSpec, ChildFragment.class, arg); 

     return root; 
    } 
} 

ChildFragment:父片段

public class ChildFragment extends SherlockFragment implements OnClickListener { 

    public static final String POSITION_KEY = "FragmentPositionKey"; 
    private int position; 

    public static ChildFragment newInstance(Bundle args) { 
     ChildFragment fragment = new ChildFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     position = getArguments().getInt(POSITION_KEY); 
     if(CommonUtils.isDebuggable) { 
      Log.d("position", "" + position); 
     } 
     View root = inflater.inflate(R.layout.fragment_child, container, false); 
     TextView textview = (TextView) root.findViewById(R.id.textViewPosition); 
     textview.setText(Integer.toString(position)); 
     textview.setOnClickListener(this); 

     return root; 
    } 

    @Override 
    public void onClick(View v) { 
     Toast.makeText(v.getContext(), "Clicked Position: " + position, Toast.LENGTH_LONG).show(); 
    } 
} 

XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.neopixl.pixlui.components.textview.TextView 
     android:id="@+id/header_text" 
     android:background="@color/menu_color" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:minHeight="@dimen/textview_height" 
     android:padding="10dp" 
     android:text="@string/wifi" 
     android:textColor="@android:color/white" 
     android:textSize="18sp" 
     pixlui:copyandpaste="false" 
     pixlui:typeface="aleo_bold.ttf" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <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="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 
     </LinearLayout> 
    </android.support.v4.app.FragmentTabHost> 

</LinearLayout> 

你能告訴我究竟是什麼問題?

回答

0

我的代碼沒有問題。我正在使用舊的android support-v4庫。然後我將庫版本更改爲最新版本(19.0.1),一切正常!