IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Lancer une Activity dans un Fragment ?


Sujet :

Android

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2013
    Messages : 5
    Points : 2
    Points
    2
    Par défaut Lancer une Activity dans un Fragment ?
    Bonsoir,

    J'aimerais savoir s'il était possible de lancer une activity dans un fragment car lorsque je créer un new intent eclipse me met une erreur en m'indiquant que le constructeur Intent(context,class) n'existe pas, je peux seulement appeler le constructeur vide.

    En fait j'ai déjà fait l'application avec des onglets mais le probleme c'est que le nom des onglet est long et qu'il sont moche aussi :



    J'aimerais pouvoir mettre autant d'onglet que je veux avec le titre que je veux et ainsi pouvoir slider au niveau des onglets de droite à gauche comme le permet le ViewPager sur eclipse lorsque l'on choisi l'option Tabs + swipe :



    Savez vous comment je peux faire ?

  2. #2
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Heu.... Oui, il suffit de récupérer le contexte... c'est à dire l'activité... donc depuis un fragment: getActivity()


    Pour les onglets... c'est une simple ActionBar
    en mode "NAVIGATION_MODE_TABS" avec les tabs rajoutés dynamiquement avec actionBar.addTab(actionBar.newTab().setText(...).setTabListener(this));
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2013
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Merci de ton aide.
    J'ai un peu avancé, hum dis moi aurais tu un exemple concret qui explique comment rajouter plusieurs fragments sur ce qu'eclipse génère automatiquement ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    package com.example.lll;
     
    import java.util.Locale;
     
    import android.app.ActionBar;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.app.NavUtils;
    import android.support.v4.view.ViewPager;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
     
    public class MainActivity extends FragmentActivity implements
    		ActionBar.TabListener {
     
    	/**
             * The {@link android.support.v4.view.PagerAdapter} that will provide
             * fragments for each of the sections. We use a
             * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
             * will keep every loaded fragment in memory. If this becomes too memory
             * intensive, it may be best to switch to a
             * {@link android.support.v4.app.FragmentStatePagerAdapter}.
             */
    	SectionsPagerAdapter mSectionsPagerAdapter;
     
    	/**
             * The {@link ViewPager} that will host the section contents.
             */
    	ViewPager mViewPager;
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
     
    		// Set up the action bar.
    		final ActionBar actionBar = getActionBar();
    		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     
    		// Create the adapter that will return a fragment for each of the three
    		// primary sections of the app.
    		mSectionsPagerAdapter = new SectionsPagerAdapter(
    				getSupportFragmentManager());
     
    		// Set up the ViewPager with the sections adapter.
    		mViewPager = (ViewPager) findViewById(R.id.pager);
    		mViewPager.setAdapter(mSectionsPagerAdapter);
     
    		// When swiping between different sections, select the corresponding
    		// tab. We can also use ActionBar.Tab#select() to do this if we have
    		// a reference to the Tab.
    		mViewPager
    				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    					@Override
    					public void onPageSelected(int position) {
    						actionBar.setSelectedNavigationItem(position);
    					}
    				});
     
    		// For each of the sections in the app, add a tab to the action bar.
    		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
    			// Create a tab with text corresponding to the page title defined by
    			// the adapter. Also specify this Activity object, which implements
    			// the TabListener interface, as the callback (listener) for when
    			// this tab is selected.
    			actionBar.addTab(actionBar.newTab()
    					.setText(mSectionsPagerAdapter.getPageTitle(i))
    					.setTabListener(this));
    		}
    	}
     
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
     
    	@Override
    	public void onTabSelected(ActionBar.Tab tab,
    			FragmentTransaction fragmentTransaction) {
    		// When the given tab is selected, switch to the corresponding page in
    		// the ViewPager.
    		mViewPager.setCurrentItem(tab.getPosition());
    	}
     
    	@Override
    	public void onTabUnselected(ActionBar.Tab tab,
    			FragmentTransaction fragmentTransaction) {
    	}
     
    	@Override
    	public void onTabReselected(ActionBar.Tab tab,
    			FragmentTransaction fragmentTransaction) {
    	}
     
    	/**
             * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
             * one of the sections/tabs/pages.
             */
    	public class SectionsPagerAdapter extends FragmentPagerAdapter {
     
    		public SectionsPagerAdapter(FragmentManager fm) {
    			super(fm);
    		}
     
    		@Override
    		public Fragment getItem(int position) {
    			// getItem is called to instantiate the fragment for the given page.
    			// Return a DummySectionFragment (defined as a static inner class
    			// below) with the page number as its lone argument.
    			Fragment fragment = new DummySectionFragment();
    			Bundle args = new Bundle();
    			args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
    			fragment.setArguments(args);
    			return fragment;
    		}
     
    		@Override
    		public int getCount() {
    			// Show 3 total pages.
    			return 3;
    		}
     
    		@Override
    		public CharSequence getPageTitle(int position) {
    			Locale l = Locale.getDefault();
    			switch (position) {
    			case 0:
    				return getString(R.string.title_section1).toUpperCase(l);
    			case 1:
    				return getString(R.string.title_section2).toUpperCase(l);
    			case 2:
    				return getString(R.string.title_section3).toUpperCase(l);
    			}
    			return null;
    		}
    	}
     
    	/**
             * A dummy fragment representing a section of the app, but that simply
             * displays dummy text.
             */
    	public static class DummySectionFragment extends Fragment {
    		/**
                     * The fragment argument representing the section number for this
                     * fragment.
                     */
    		public static final String ARG_SECTION_NUMBER = "section_number";
     
    		public DummySectionFragment() {
    		}
     
    		@Override
    		public View onCreateView(LayoutInflater inflater, ViewGroup container,
    				Bundle savedInstanceState) {
    			View rootView = inflater.inflate(R.layout.fragment_main_dummy,
    					container, false);
    			TextView dummyTextView = (TextView) rootView
    					.findViewById(R.id.section_label);
    			dummyTextView.setText(Integer.toString(getArguments().getInt(
    					ARG_SECTION_NUMBER)));
    			return rootView;
    		}
    	}
     
    }
    J'ai essayé à plusieurs reprise mais je n'arrive pas à rajouter des fragments je ne vois même pas comment faire ...

Discussions similaires

  1. Lancer une Activity à l'aide d'un bouton qui se trouve dans un FragmentActivity
    Par FunckyWarrior dans le forum Composants graphiques
    Réponses: 13
    Dernier message: 30/07/2013, 12h51
  2. Lancer une Activity selon l'item cliqué dans une ListView
    Par LOLPLZ dans le forum Composants graphiques
    Réponses: 4
    Dernier message: 10/03/2013, 23h09
  3. Réponses: 9
    Dernier message: 25/01/2006, 15h35
  4. Réponses: 10
    Dernier message: 04/12/2005, 23h12
  5. Comment lancer une erreur dans une procédure stockée
    Par borgfabr dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 17/05/2005, 17h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo