Oli
Oli

Reputation: 398

Fragments not shown when using Navigation Tabs with ActionBarSherlock

I'm trying to use Navigation Tabs to change between some Fragments as like as you can do by using a Tabhost.
Unfortunately the Tabs are shown in the ActionBar, but the content of the fragments aren't. Here's my code for the MainActivity:

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends SherlockFragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    Tab tab = bar
            .newTab()
            .setText("Tab1")
            .setTabListener(new MyTabListener(this, new Fragment1()));
    bar.addTab(tab);

    tab = bar
            .newTab()
            .setText("Tab2")
            .setTabListener(new MyTabListener(this, new Fragment2()));
    bar.addTab(tab);
}

public static class MyTabListener implements TabListener {

    private MainActivity mActivity;
    private Fragment mFragment;

    public MyTabListener(MainActivity activity, Fragment fragment) {
        mActivity = activity;
        mFragment = fragment;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ignoredFt) {
        FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.content, mFragment, null);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}
}

Fragment1.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

}

fragment1.xml:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

main.xml:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/content" >
</LinearLayout>

</LinearLayout>

Fragment2 has the same structure as Fragment1 but other Text in TextView!
So it seems that the Fragment's View can't be shown in content of main.xml.

I hope you can help me...

Upvotes: 1

Views: 2816

Answers (1)

Ollie C
Ollie C

Reputation: 28509

It looks like you're missing the FragmentTransaction.commit()

Upvotes: 3

Related Questions