CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: gianpassa con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: carotix con ben 11 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 26 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 1 su 1
Discussione:

[SNIPPET] Aggiunta dinamica di Fragments a ViewPager

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Androidiani Power User L'avatar di cesco


    Registrato dal
    Sep 2010
    Località
    Bg
    Messaggi
    5,018
    Smartphone
    TYTN2,desire,desireHD, oneX

    Ringraziamenti
    54
    Ringraziato 1,346 volte in 724 Posts
    Predefinito

    [SNIPPET] Aggiunta dinamica di Fragments a ViewPager

    Dato che a volte nel ViewPager vi è la necessità di aggiungere fragments dinamicamente, ho pensato fosse una buona idea condividere questi frammenti di codice
    Possono tornare utili in diverse situazioni

    Il tutto è postato in un Gist su github, scaricabile qui: https://gist.github.com/cesco89/11008973

    Qui sotto trovate comunque l'embed del gist sopra linkato

    List<Fragment> fragments = buildFragments();
    ArrayList<String> categories = {"1", "2", "3", ..., "n"};
    mPager = (ViewPager) v.findViewById(R.id.pager);
    mPageAdapter = new MyFragmentPageAdapter(this,getSupportFragmentManager(), fragments, categories);
    mPager.setAdapter(mPageAdapter);
    //Add a new Fragment to the list with bundle
    Bundle b = new Bundle();
    b.putInt("position", i);
    String title = "asd";
    mPageAdapter.add(MyFragment.class, title, b);
    mPageAdapter.notifyDataSetChanged();
    import java.util.ArrayList;
    import java.util.List;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    public class MyFragmentPageAdapter extends FragmentPagerAdapter {
    public static int pos = 0;
    private List<Fragment> myFragments;
    private ArrayList<String> categories;
    private Context context;
    public MyFragmentPageAdapter(Context c, FragmentManager fragmentManager, List<Fragment> myFrags, ArrayList<String> cats) {
    super(fragmentManager);
    myFragments = myFrags;
    this.categories = cats;
    this.context = c;
    }
    @Override
    public Fragment getItem(int position) {
    return myFragments.get(position);
    }
    @Override
    public int getCount() {
    return myFragments.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {
    setPos(position);
    return categories.get(position);
    }
    public static int getPos() {
    return pos;
    }
    public void add(Class<Fragment> c, String title, Bundle b) {
    myFragments.add(Fragment.instantiate(context,c.getName(),b));
    categories.add(title);
    }
    public static void setPos(int pos) {
    MyFragmentPageAdapter.pos = pos;
    }
    }
    private List<android.support.v4.app.Fragment> buildFragments() {
    List<android.support.v4.app.Fragment> fragments = new ArrayList<android.support.v4.app.Fragment>();
    for(int i = 0; i<categories.size(); i++) {
    Bundle b = new Bundle();
    b.putInt("position", i);
    fragments.add(Fragment.instantiate(this,MyPagerFragment.class.getName(),b));
    }
    return fragments;
    }
    Follow me on:
    <!-- Place this tag where you want the widget to render. -->
    <div class="g-person" data-width="180" data-href="//plus.google.com/113012341277613226011" data-theme="dark" data-rel="author"></div>

    <!-- Place this tag after the last widget tag. -->
    <script type="text/javascript">
    window.___gcfg = {lang: 'it'};

    (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();
    </script>
    <br>
    <a class="twitter-timeline" href="https://twitter.com/xcesco89" data-widget-id="398762031488040960">Tweets di @xcesco89</a>
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementB yId(id)){js=d.createElement(s);js.id=id;js.src=p+" ://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}} (document,"script","twitter-wjs");</script>
    Androidiani app
    Estensione Androidiani per Google Chrome

  2. I seguenti 2 Utenti hanno ringraziato cesco per il post:

    Crotan (18-04-14),nikita1977 (18-04-14)

  3.  

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy