Added Tabnavigation to main Activity

This commit is contained in:
Daniel Oeh 2012-05-29 14:05:47 +02:00
parent 92c53bcf92
commit ed3890b469

View File

@ -15,8 +15,12 @@ import android.widget.ListView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.os.Bundle; import android.os.Bundle;
import android.content.Intent; import android.content.Intent;
import android.app.Activity;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
@ -32,13 +36,56 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); setContentView(R.layout.main);
FragmentManager fragmentManager = getSupportFragmentManager(); // Set up tabs
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ActionBar actionBar = getSupportActionBar();
feedlist = new FeedlistFragment(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
fragmentTransaction.replace(R.id.main_fragment, feedlist); actionBar.setDisplayShowTitleEnabled(false);
fragmentTransaction.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
Tab tab = actionBar.newTab()
.setText(getText(R.string.feeds_label).toString())
.setTabListener(new TabListener<FeedlistFragment>(
this, getText(R.string.feeds_label).toString(), FeedlistFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Playlist")
.setTabListener(new TabListener<FeedlistFragment>(
this, "Playlist", FeedlistFragment.class));
actionBar.addTab(tab);
}
/** TabListener for navigating between the main lists. */
private class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity activity;
private final String tag;
private final Class<T> fClass;
private Fragment fragment;
public TabListener(Activity activity, String tag, Class<T> fClass) {
this.activity = activity;
this.tag = tag;
this.fClass = fClass;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment == null) {
fragment = Fragment.instantiate(activity, fClass.getName());
ft.replace(R.id.main_fragment, fragment);
} else {
ft.attach(fragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.detach(fragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// Do nothing
}
} }
} }