Jump to content

Avis sur les Tabs


jpr928

Recommended Posts

Bonjour, je test les tabActivity et je voulais savoir si il est possible d'afficher les fragmentactivity des tab dans un multi-panel

Je dis ça pour les tablettes. Ou bien y a t'il qqchose de mieux ?

Merci d'avance

 

post-156481-0-06330200-1399297034_thumb.

Link to comment
Share on other sites

Bonjour,

 

TabActivity est dépréciée, et il n'est plus recommandé de l'utiliser. C'est maintenant l'ActionBar qui gère les onglets, dans le cas général.

Dans ton cas, si tu veux gérer les onglets au niveau d'un fragment, tu as FragmentTabHost de la librairie de support qui te le permet.

Link to comment
Share on other sites

Bon voila j'ai mon code ActionBar mais comment je peut faire pour l'afficher dans le Fragment central (j'ai fait l'activity des 3 fragments) ?

Est-ce possible avec ActionBar ?

package com.lajares;


import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
 
public class MainActivity extends Activity {
 
    @[member=override]
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        ActionBar actionBar = getActionBar();
 
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
 
        String l1 = getResources().getString(R.string.l1);
        Tab tab = actionBar.newTab();
        tab.setText(l1);
        TabListener<Tab1Fragment> tl = new TabListener<Tab1Fragment>(this,
                l1, Tab1Fragment.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);
 
        String l2 = getResources().getString(R.string.l2);
        tab = actionBar.newTab();
        tab.setText(l2);
        TabListener<Tab2Fragment> tl2 = new TabListener<Tab2Fragment>(this,
                l2, Tab2Fragment.class);
        tab.setTabListener(tl2);
        actionBar.addTab(tab);
        
        String l3 = getResources().getString(R.string.l3);
        tab = actionBar.newTab();
        tab.setText(l3);
        new TabListener<Tab3Fragment>(this,
                l3, Tab3Fragment.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);
        
        String l4 = getResources().getString(R.string.l4);
        tab = actionBar.newTab();
        tab.setText(l4);
        new TabListener<Tab4Fragment>(this,
                l4, Tab4Fragment.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);
        
        String l5 = getResources().getString(R.string.l5);
        tab = actionBar.newTab();
        tab.setText(l5);
        new TabListener<Tab5Fragment>(this,
                l5, Tab5Fragment.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);
        
        String l6 = getResources().getString(R.string.l6);
        tab = actionBar.newTab();
        tab.setText(l6);
        new TabListener<Tab6Fragment>(this,
                l6, Tab6Fragment.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);
        
        String l7 = getResources().getString(R.string.l7);
        tab = actionBar.newTab();
        tab.setText(l7);
        new TabListener<Tab7Fragment>(this,
                l7, Tab7Fragment.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);
 
    }
 
    private 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;
        }
 
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
		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);
            }
        }
 
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
		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.
        }
    }
 
}

Une idée de la direction ou je dois chercher ?

Merci

Link to comment
Share on other sites

Une ActionBar ne s'affiche pas dans un Fragment, une ActionBar est générale à une Activity. Comme déjà dit, si tu veux gérer des onglets dans un de tes fragments, ce n'est pas par l'ActionBar qu'il faut passer, mais plutôt utiliser un FragmentTabHost ...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...