在Android中的TabHost中显示片段

时间:2015-03-23 16:29:52

标签: android android-fragments tabs fragment android-tabhost

我有一个有两个标签的应用程序,我想在每个标签中显示一个片段。我在包含选项卡的片段中添加了一个TabHost。标签看起来很像这样:

http://i.stack.imgur.com/lzTyF.png(抱歉,我无法发布图片)

但问题是内容(片段)没有在FrameLayout中显示。

此片段的代码是:

BecasEmpleoFragment.java

private static final String ARG_SECTION_NUMBER = "section_number"; // Parámetro necesario
private static final String TAG1 = "estudiantes";
private static final String TAG2 = "profesionales";

private TabHost mTabs;
private View mRoot;
private int mCurrentTab;

/**
 * Retorna una nueva instancia del fragmento que indica el numero de seccion indicado
 */
public static BecasEmpleoFragment newInstance(int sectionNumber) {
    BecasEmpleoFragment fragment = new BecasEmpleoFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public BecasEmpleoFragment() {
    // Constructor por defecto.
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Infla el fragment.
    mRoot = inflater.inflate(R.layout.fragment_becas_empleo, container, false);

    // Se crean las pestañas
    mTabs=(TabHost)mRoot.findViewById(R.id.tabhost);

    setupTabs();

    // Se cambia el color y la posición de los textos de la pestaña.
    for (int tabIndex = 0 ; tabIndex < mTabs.getTabWidget().getTabCount() ; tabIndex ++) {
        View tab = mTabs.getTabWidget().getChildTabViewAt(tabIndex);
        TextView t = (TextView)tab.findViewById(android.R.id.title);
        t.setTextColor(getResources().getColor(android.R.color.white));
        t.setGravity(Gravity.CENTER);
    }

    return mRoot;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    mTabs.setOnTabChangedListener(this);
    mTabs.setCurrentTab(mCurrentTab);
    updateTab(TAG1, R.id.tab1);
}

private void setupTabs(){
    mTabs.setup();
    mTabs.addTab(newTab(TAG1, R.string.estudiantes, R.id.tab1));
    mTabs.addTab(newTab(TAG2, R.string.profesionales, R.id.tab2));
}

private TabHost.TabSpec newTab(String tag, int title, int tabContentId){
    TabHost.TabSpec tabSpec = mTabs.newTabSpec(tag);
    tabSpec.setIndicator(getResources().getString(title));
    tabSpec.setContent(tabContentId);
    return tabSpec;
}

private void updateTab(String tabId, int placeholder){
    FragmentManager fm = getFragmentManager();
    if(fm.findFragmentByTag(tabId) == null){
        switch(tabId){
            case TAG1:
                Log.v("Replace", TAG1);
                fm.beginTransaction()
                        .replace(placeholder, new BecasEstudiantesFragment(), tabId).commit();
                break;
            case TAG2:
                Log.v("Replace", TAG2);
                fm.beginTransaction()
                        .replace(placeholder, new RedesSocialesFragment(), tabId).commit();
                break;
        }
    }
}

@Override
public void onTabChanged(String tabId){
    Log.d("MNB", "Changing to tab: " + tabId);
    if(TAG1.equals(tabId)){
        Log.v("Actualizo", TAG1);
        updateTab(tabId, R.id.tab1);
        mCurrentTab = 0;
        return;
    }
    if(TAG2.equals(tabId)){
        Log.v("Actualizo", TAG2);
        updateTab(tabId, R.id.tab2);
        mCurrentTab = 1;
        return;
    }
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
}

他的XML文件:

fragment_becas_empleo.xml

<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/setting"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:background="@color/blue" />

    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout android:id="@+id/tab1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="46dp"/>

        <FrameLayout android:id="@+id/tab2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="46dp"/>

    </FrameLayout>

</LinearLayout>

在我要在tab1中膨胀的片段的onCreateView方法中,我写了一个日志:

BecasEstudiantesFragment.java

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.v("Dibujando", "Becas Estudiantes");
    return inflater.inflate(R.layout.fragment_becas_estudiantes, container, false);
}

在logcat中,我可以看到我正在膨胀的片段进入他的onCreateView方法。

03-23 17:00:21.302  19497-19562/es.uniovi.epi D/dalvikvm﹕ GC_FOR_ALLOC freed 5268K, 10% free 58538K/64384K, paused 53ms, total 53ms
03-23 17:00:21.302  19497-19562/es.uniovi.epi I/dalvikvm-heap﹕ Grow heap (frag case) to 59.181MB for 1260016-byte allocation
03-23 17:00:23.043  19497-19497/es.uniovi.epi V/Replace﹕ estudiantes
03-23 17:00:23.043  19497-19497/es.uniovi.epi V/Dibujando﹕ Becas Estudiantes

我做错了什么?

谢谢!

0 个答案:

没有答案