Added Tabnavigation to main Activity

This commit is contained in:
Daniel Oeh 2012-05-29 14:05:47 +02:00
parent 92c53bcf92
commit ed3890b469
1 changed files with 55 additions and 8 deletions

View File

@ -15,8 +15,12 @@ import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import com.actionbarsherlock.app.SherlockFragmentActivity;
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.FragmentTransaction;
@ -31,14 +35,57 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up tabs
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
feedlist = new FeedlistFragment();
fragmentTransaction.replace(R.id.main_fragment, feedlist);
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
}
}
}