22

Ho ottenuto un FragmentPagerAdapter. Il metodo getItem può restituire un frammento in base ai dati che ha dall'esterno. Dopo aver aggiornato i dati, supponiamo di visualizzarli. Io chiamo notifyDataSetChanged eppure non è successo niente. HO FATTO override del metodo getItemPosition per tornare POSITION_NONE:FragmentPagerAdapter notifyDataSetChanged not working

public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, 
     ViewPager.OnPageChangeListener 
{ 
    private final Context mContext; 
    private final TabHost mTabHost; 
    private final ViewPager mViewPager; 
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); 
    private PagerTabsFragments fragmentDisplayInfo = null; // for now only a 
                  // single size 
                  // back 
                  // stack 

    static class PagerTabsFragments 
    { 
     public int tabPosition; 
     public Fragment fragmentToDisplay; 
     public Object fragmentInfo; 

     public PagerTabsFragments(int tab, Fragment frag, Object info) 
     { 
      tabPosition = tab; 
      fragmentToDisplay = frag; 
      fragmentInfo = info; 
     } 
    } 

    public void SetFragmentToDisplay(int tabPosition, Fragment frag, Object info) 
    { 
     fragmentDisplayInfo = new PagerTabsFragments(tabPosition, frag, info); 
     notifyDataSetChanged(); 
    } 

    public void CancelFragmentToDisplay() 
    { 
     fragmentDisplayInfo = null; 
    } 
... 
@Override 
    public Fragment getItem(final int position) 
    { 
     if ((fragmentDisplayInfo != null) && (fragmentDisplayInfo.tabPosition == position)) 
     { 
      return fragmentDisplayInfo.fragmentToDisplay; 
     } 
     final TabInfo info = mTabs.get(position); 
     return Fragment.instantiate(mContext, info.clss.getName(), info.args); 
    } 

    @Override 
    public int getItemPosition(Object item) 
    { 
     if (fragmentDisplayInfo == null) 
     { 
      return super.getItemPosition(item); 
     } 
     return POSITION_NONE; 
    } 
... 

e l'aggiornamento dall'esterno è in un evento click che imposta il fragmentDisplayInfo var. Ho eseguito il debug e ho visto che lo fragmentDisplayInfo è effettivamente inizializzato e il metodo getItemPosition restituisce POSITION_NONE, ma il metodo getItem non viene nemmeno chiamato. Perché?

EDIT:

Se non avessi fatto così così, si prega di notare che parte overiden dell'adattatore:

@Override 
public int getItemPosition(Object item) 
{ 
    return POSITION_NONE; 
} 
+0

hai trovato una soluzione alla fine? – AndroidEnthusiast

risposta

33

Non sono sicuro di questo, ma si può provare a utilizzare FragmentStatePagerAdapter anziché FragmentPagerAdapter. Il fatto è che ho anche incontrato questo problema, e mi ha aiutato

42

Ciò che Nik Myers dice è corretto. Tuttavia manca un pezzo. Quando viene chiamato notifyDataSetChanged, viene chiamato il metodo getItemPosition. È necessario sovrascriverlo per far ricaricare i frammenti.

@Override 
    public int getItemPosition(Object object) { 
     // Causes adapter to reload all Fragments when 
     // notifyDataSetChanged is called 
     return POSITION_NONE; 
    } 
0

Ho affrontato il problema simile quando stavo lavorando al mio ultimo progetto. Quindi ho trovato poche soluzioni.

  1. sovrascrivendo getItemPosition nell'adattatore del cercapersone ma questa non è una buona idea.

    @Override 
    public int getItemPosition(Object object) { 
        // it will recreate all Fragments when 
        // notifyDataSetChanged is called 
        return POSITION_NONE; 
    } 
    
  2. Il secondo è cabezas soluzione.

  3. più stabile soluzione di override getItemPosition nella moda di seguito:

    @Override 
        public int getItemPosition(Object object) { 
         if (object instanceof MyFragment) { 
          // Create a new method notifyUpdate() in your fragment 
          // it will get call when you invoke 
          // notifyDatasetChaged(); 
          ((MyFragment) object).notifyUpdate(); 
         } 
         //don't return POSITION_NONE, avoid fragment recreation. 
         return super.getItemPosition(object); 
        }