Trouble using ActionBarSherlock Tab Navigation to display a Fragment inside a FrameLayout by ID

I'm having such trouble trying to figure this out.. I've looked in the SampleFragments ABS app and it uses TabHost with a custom Listener to accomplish this.. Is this my only option or can I make it work how I'm trying to make it work.. (I want to use the default TabNav due to how it overlays on the ActionBar in Landscape mode to save screen space.

MainActivity:

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create Tab Nav
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    bar.addTab(bar.newTab()
            .setText("Tab1")
            .setTabListener(this));        
    bar.addTab(bar.newTab()
            .setText("Tab2")
            .setTabListener(this)); 
    bar.addTab(bar.newTab()
            .setText("Tab3")
            .setTabListener(this));
}

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

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {

    FrameLayout frame = (FrameLayout) findViewById(R.id.tabcontent);
    if (tab.getText() == "Tab2")
    {           
        frame.setBackgroundColor(0xFF00FF00);
    }
    if (tab.getText() == "Tab1")
    {
        frame.setBackgroundColor(0xFFFFFFFF);
    }        
}

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

Support & Fragment Activity

public class TestSupport extends SherlockFragmentActivity{

public static Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn = (Button) findViewById(R.id.buttonOMG);

    FragmentManager fm = getSupportFragmentManager();

    // Create the list fragment and add it as our sole content.
    if (fm.findFragmentById(android.R.id.tabcontent) == null) {
        MapsFragment list = new MapsFragment();
        fm.beginTransaction().add(android.R.id.tabcontent, list).commit();
    }
}

public static class TestFragment extends SherlockListFragment {

    @Override public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //setHasOptionsMenu(true);
        btn.setText("LOL");            

    }
}
}

Main.xml

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

<FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    <Button
        android:id="@+id/buttonOMG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</FrameLayout>
</LinearLayout>

I'm just using the change of the button text as an indicator to see if it implements correctly..

What I'm having trouble with is in the onTabSelected class.. How do I call the fragment to execute and make it show on the tabcontent layout? Is my fragment & support coded correctly to where I just need to call it to make it work? I'm stumped.

Thank you!

Answers


Need Your Help

MVC 4 Controller action parameter System.FormatException bug

exception asp.net-mvc-4 kendo-grid

We are finding that an ajax controller action which backs a KendoUI grid is throwing a lot of System.FormatException exceptions, see below.

Does UPS Address Validation API works in mobile app?

validation street-address ups

I am learning UPS address validation API for my PHP application. Going to use web service version for my integration. However I am not sure whether UPS address validation will work for mobile app o...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.