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

Composants graphiques Android Discussion :

ViewPager dans un fragment


Sujet :

Composants graphiques Android

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Novembre 2012
    Messages : 6
    Par défaut ViewPager dans un fragment
    Bonjour,

    Pour l'instant ce que je montre depuis ma fragment list sont de simple layouts, par :
    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
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            String curPage = getArguments().getString(ARG_ITEM_ID);
            View rootView;
            if(curPage.equals("1")) {
            	rootView=inflater.inflate(R.layout.fragment_item_detail1, container, false);
            } else if (curPage.equals("2")) {
            	rootView=inflater.inflate(R.layout.conf_activity, container, false);
            } else if (curPage.equals("3")) {
            	rootView=inflater.inflate(R.layout.fragment_item_detail3, container, false);
            } else if (curPage.equals("4")) {
            	rootView=inflater.inflate(R.layout.fragment_item_detail4, container, false);
            } else {
            	rootView=inflater.inflate(R.layout.fragment_item_detail5, container, false);
            	TextView t1 = (TextView) rootView.findViewById(R.id.acces_addr_isfa);
                t1.setMovementMethod(LinkMovementMethod.getInstance());
                TextView t2 = (TextView) rootView.findViewById(R.id.acces_addr_gala);
                t2.setMovementMethod(LinkMovementMethod.getInstance());
                final Button goIsfa = (Button) rootView.findViewById(R.id.button_isfa);
                goIsfa.setOnClickListener(new OnClickListener() {
                	public void onClick(View v) {
                		Uri location1 = Uri.parse("geo:0,0?q=50+avenue+Tony+Garnier,+69007+Lyon");
                		Intent intent1 = new Intent(Intent.ACTION_VIEW, location1);
                		String title = "Choisissez votre app de navigation préférée";
                		// Create and start the chooser
                		startActivity(Intent.createChooser(intent1, title));
                	}
                });
                final Button goGala = (Button) rootView.findViewById(R.id.button_gala);
                goGala.setOnClickListener(new OnClickListener() {
                	public void onClick(View v) {
                		Uri location2 = Uri.parse("geo:0,0?q=Les+Terrasses+du+Parc,+115+bd+Stalingrad,+69100+Villeurbanne");
                		Intent intent2 = new Intent(Intent.ACTION_VIEW, location2);
                		String title = "Choisissez votre app de navigation préférée";
                		// Create and start the chooser
                		startActivity(Intent.createChooser(intent2, title));
                	}
                });
            }
            return rootView;
        }
    Seulement voilà, j'aimerais bien insérer un ViawPager, défini par un FragmentActivity à la place d'un simple layout.
    En mode portrait ça se pase très bien par un intent, mais en mode paysage... je sèche.

    J'avais dans l'idée de faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    if(id.equals("2")) {
                	ConfActivity fragment = new ConfActivity();
                	getSupportFragmentManager().beginTransaction()
                    	.replace(R.id.item_detail_container, fragment)
                    	.commit();
                }
    Dans le .itemSelected de ma FragmentActivity, mais vu que fragment est une FragmentActivity, ça ne fonctionne pas.

    D'avance merci !

  2. #2
    Membre éclairé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2011
    Messages
    67
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Finance

    Informations forums :
    Inscription : Novembre 2011
    Messages : 67
    Par défaut
    Ce que tu fais là ne ressemble pas vraiment à l'utilisation d'une liste de fragment.

    En fait tu utilises un Fragment qui inflate un Layout différent en fonction d'un paramètre ?

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Novembre 2012
    Messages : 6
    Par défaut
    Tu as raison, le menu est dans un extends de FragmentActivity et ce sont les ajouts qui sont dans un extends de ListFragment.
    Voilà le code de ce que je veux insérer :
    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
    package com.dwiimdev.forumisfa2012;
     
    import java.util.Calendar;
     
    import android.content.Intent;
    import android.content.res.Configuration;
    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.FragmentStatePagerAdapter;
    import android.support.v4.app.NavUtils;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
     
    public class ConfActivity extends FragmentActivity {
     
        /** 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;
     
        private int mActivatedPosition = ListView.INVALID_POSITION;
        private static final String STATE_ACTIVATED_POSITION = "activated_position";
     
        /** The {@link ViewPager} that will host the section contents. */
        ViewPager mViewPager;
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_confs);
     
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                // If the screen is now in landscape mode, we can show the
                // dialog in-line with the list so we don't need this activity.
            	savedInstanceState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
                finish();
                return;
            }
     
            getActionBar().setDisplayHomeAsUpEnabled(true);
            setTitle(R.string.title_detail2);
            // 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);
     
            Calendar xx = Calendar.getInstance();
            int jour = xx.get(Calendar.DAY_OF_MONTH);
            int mois = xx.get(Calendar.MONTH)+1;
            int annee = xx.get(Calendar.YEAR);
            if ((jour==16)&&(mois==11)&&(annee==2012)) {
            	mViewPager.setCurrentItem(1);
            }
        }
     
        /** A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary sections of the app. */
        public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
     
            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }
     
            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                fragment.setArguments(args);
                return fragment;
            }
     
            @Override
            public int getCount() {
                return 3;
            }
     
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
    	            case 0: return getString(R.string.title_conf1);
    	            case 1: return getString(R.string.title_conf2);
    	            case 2: return getString(R.string.title_conf3);
                }
                return null;
            }
        }
     
        /** A dummy fragment representing a section of the app, but that simply displays dummy text. */
        public static class DummySectionFragment extends Fragment {
            public DummySectionFragment() {
            }
     
            public static final String ARG_SECTION_NUMBER = "section_number";
     
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
            	Bundle args = getArguments();
                int curPage = args.getInt(ARG_SECTION_NUMBER);
                View rootView;
                if(curPage == 1) {
                	rootView=inflater.inflate(R.layout.activity_conf1, container, false);
                } else if (curPage == 2) {
                	rootView=inflater.inflate(R.layout.activity_conf2, container, false);
                } else {
                	rootView=inflater.inflate(R.layout.activity_conf3, container, false);
                }
                return rootView;
            }
        }
     
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == android.R.id.home) {
                NavUtils.navigateUpTo(this, new Intent(this, ItemListActivity.class));
                return true;
            }
     
            return super.onOptionsItemSelected(item);
        }
    }
    Celui du menu

    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
    package com.dwiimdev.forumisfa2012;
     
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.widget.ListView;
     
    public class ItemListActivity extends FragmentActivity
            implements ItemListFragment.Callbacks {
     
        private boolean mTwoPane;
        private int mActivatedPosition = ListView.INVALID_POSITION;
        private static final String STATE_ACTIVATED_POSITION = "activated_position";
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_item_list);
     
            if (findViewById(R.id.item_detail_container) != null) {
                mTwoPane = true;
                ((ItemListFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.item_list))
                        .setActivateOnItemClick(true);
            }
        }
     
        @Override
        public void onItemSelected(String id) {
            if (mTwoPane) {
                Bundle arguments = new Bundle();
                arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
                /*if(id.equals("2")) {
                	ConfActivity fragment = new ConfActivity();
                } else if(id.equals("3")) {
                	StandsActivity fragment = new StandsActivity();
                } else {*/
                	ItemDetailFragment fragment = new ItemDetailFragment();
                    fragment.setArguments(arguments);
                    getSupportFragmentManager().beginTransaction()
                         .replace(R.id.item_detail_container, fragment)
                         .commit();
                //}
            } else {
            	Intent detailIntent;
            	if (id.equals("2")) {
            		detailIntent = new Intent(ItemListActivity.this, ConfActivity.class);
            	} else if (id.equals("3")) {
            		detailIntent = new Intent(ItemListActivity.this, StandsActivity.class);
            	}
            	else {
            		detailIntent = new Intent(this, ItemDetailActivity.class);
            		detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
            	}
                startActivity(detailIntent);
            }
        }
     
        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            if (mActivatedPosition != ListView.INVALID_POSITION) {
                outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
            }
        }
    }
    Avec celui-là en mode paysage
    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
    package com.dwiimdev.forumisfa2012;
     
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.widget.ListView;
     
    public class ItemListActivity extends FragmentActivity
            implements ItemListFragment.Callbacks {
     
        private boolean mTwoPane;
        private int mActivatedPosition = ListView.INVALID_POSITION;
        private static final String STATE_ACTIVATED_POSITION = "activated_position";
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_item_list);
     
            if (findViewById(R.id.item_detail_container) != null) {
                mTwoPane = true;
                ((ItemListFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.item_list))
                        .setActivateOnItemClick(true);
            }
        }
     
        @Override
        public void onItemSelected(String id) {
            if (mTwoPane) {
                Bundle arguments = new Bundle();
                arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
                /*if(id.equals("2")) {
                	ConfActivity fragment = new ConfActivity();
                } else if(id.equals("3")) {
                	StandsActivity fragment = new StandsActivity();
                } else {*/
                	ItemDetailFragment fragment = new ItemDetailFragment();
                    fragment.setArguments(arguments);
                    getSupportFragmentManager().beginTransaction()
                         .replace(R.id.item_detail_container, fragment)
                         .commit();
                //}
            } else {
            	Intent detailIntent;
            	if (id.equals("2")) {
            		detailIntent = new Intent(ItemListActivity.this, ConfActivity.class);
            	} else if (id.equals("3")) {
            		detailIntent = new Intent(ItemListActivity.this, StandsActivity.class);
            	}
            	else {
            		detailIntent = new Intent(this, ItemDetailActivity.class);
            		detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
            	}
                startActivity(detailIntent);
            }
        }
     
        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            if (mActivatedPosition != ListView.INVALID_POSITION) {
                outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
            }
        }
    }
    Merci !

Discussions similaires

  1. CountDownTimer dans chaque Fragment avec ViewPager
    Par miniil dans le forum Composants graphiques
    Réponses: 0
    Dernier message: 22/05/2014, 14h00
  2. [GLSL] Texture2D dans un fragment shader
    Par GloW_on_dub dans le forum OpenGL
    Réponses: 7
    Dernier message: 12/04/2010, 15h50
  3. Récupérer la position dans un fragment sh.
    Par Bakura dans le forum OpenGL
    Réponses: 4
    Dernier message: 21/11/2007, 17h23
  4. recuperation des textures dans un fragment program
    Par vieurou dans le forum OpenGL
    Réponses: 5
    Dernier message: 17/08/2007, 18h20
  5. Insérer une image dans un fragment de page jspf
    Par ze veritable farf dans le forum Servlets/JSP
    Réponses: 7
    Dernier message: 09/05/2006, 10h43

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