Wednesday, May 16, 2012

22. ActionBar navigation with Fragments

Fragments can also be used in combination with the ActionBar for navigation. For this your main Activity needs to implement a TabListener which is responsible for moving between the tabs.
The ActionBar allows to add tabs to it via the newTab() method. The following code shows such an Activity. It uses two Fragments, called DetailFragment and ImageFragment. At this point you should be able to create these two Fragments yourself.
   
package de.vogella.android.fragment;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // setup action bar for tabs
  ActionBar actionBar = getActionBar();
  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  actionBar.setDisplayShowTitleEnabled(false);

  Tab tab = actionBar
    .newTab()
    .setText("First tab")
    .setTabListener(
      new MyTabListener<DetailFragment>(this, "artist",
        DetailFragment.class));
  actionBar.addTab(tab);

  tab = actionBar
    .newTab()
    .setText("Second Tab")
    .setTabListener(
      new MyTabListener<ImageFragment>(this, "album",
        ImageFragment.class));
  actionBar.addTab(tab);

 }

 public static class MyTabListener<T extends Fragment> implements
   TabListener {
  private Fragment mFragment;
  private final Activity mActivity;
  private final String mTag;
  private final Class<T> mClass;

  
/** * Constructor used each time a new tab is created. * * @param activity * The host Activity, used to instantiate the fragment * @param tag * The identifier tag for the fragment * @param clz * The fragment's Class, used to instantiate the fragment */
public MyTabListener(Activity activity, String tag, Class<T> clz) { mActivity = activity; mTag = tag; mClass = clz; } /* The following are each of the ActionBar.TabListener callbacks */ public void onTabSelected(Tab tab, FragmentTransaction ft) { // Check if the fragment is already initialized if (mFragment == null) { // If not, instantiate and add it to the activity mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, mFragment, mTag); } else { // If it exists, simply attach it in order to show it ft.setCustomAnimations(android.R.animator.fade_in, R.animator.animationtest); ft.attach(mFragment); } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (mFragment != null) { ft.setCustomAnimations(android.R.animator.fade_in, R.animator.test); ft.detach(mFragment); } } public void onTabReselected(Tab tab, FragmentTransaction ft) { } } }

No comments:

Post a Comment