Tecnologia do Blogger.
RSS

[androidbrasil-dev] Navegação Tabs Android 4 por Activity

Pessoal,

Anteriormente para navegar entre tabs possui-a a classe TabActivity da seguinte forma:

public class TabHostTeste extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
          
            TabHost tabHost = getTabHost();  // The activity TabHost
            tabHost = getTabHost();  // The activity TabHost
            TabHost.TabSpec spec;  // Resusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab
           
            // Initialize a TabSpec for each tab and add it to the TabHost
            intent = new Intent().setClass(this, Teste1CadActivity.class);
            spec = tabHost.newTabSpec("Teste1").setIndicator("Aba Teste 1").setContent(intent);
            tabHost.addTab(spec);

            // Initialize a TabSpec for each tab and add it to the TabHost
            intent = new Intent().setClass(this, Teste2CadActivity.class);
            spec = tabHost.newTabSpec("Teste2").setIndicator("Aba Teste 2").setContent(intent);
            tabHost.addTab(spec);
    }
}

Porem ao criar um novo projeto com a versão 4 do android, é informado que a classe TabActivity esta "deprecated", na documentação http://developer.android.com/guide/topics/ui/actionbar.html parece que agora deve ser utilizado a ActionBar e implementar a classe ActionBar.TabListener.

@Override
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
   
// Notice that setContentView() is not used, because we use the root
   
// android.R.id.content as the container for each fragment

   
// setup action bar for tabs
   
ActionBar actionBar = getActionBar();
    actionBar
.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar
.setDisplayShowTitleEnabled(false);

   
Tab tab = actionBar.newTab()
           
.setText(R.string.artist)
           
.setTabListener(new TabListener<ArtistFragment>(
                   
this, "artist", ArtistFragment.class));
    actionBar
.addTab(tab);

    tab
= actionBar.newTab()
       
.setText(R.string.album)
       
.setTabListener(new TabListener<AlbumFragment>(
               
this, "album", AlbumFragment.class));
    actionBar
.addTab(tab);
}

public static class TabListener<T extends Fragment> implements ActionBar.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 TabListener(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
.attach(mFragment);
       
}
   
}

   
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
       
if (mFragment != null) {
           
// Detach the fragment, because another one is being attached
            ft
.detach(mFragment);
       
}
   
}

   
public void onTabReselected(Tab tab, FragmentTransaction ft) {
       
// User selected the already selected tab. Usually do nothing.
   
}
}

Sendo assim gostaria de saber se para utilizar estas abas no Android 4 é necessario alterar todas as Activity em Fragment? Não é possivel utilizar minha estrutura anterior mantendo minhas Activity's para navegação?

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário