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;
@ -31,14 +35,57 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); setContentView(R.layout.main);
// Set up tabs
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
FragmentManager fragmentManager = getSupportFragmentManager(); Tab tab = actionBar.newTab()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); .setText(getText(R.string.feeds_label).toString())
feedlist = new FeedlistFragment(); .setTabListener(new TabListener<FeedlistFragment>(
fragmentTransaction.replace(R.id.main_fragment, feedlist); this, getText(R.string.feeds_label).toString(), FeedlistFragment.class));
fragmentTransaction.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN); actionBar.addTab(tab);
fragmentTransaction.commit(); 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
}
}
} }