Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentPagerAdapter notifyDataSetChanged not working

I got a FragmentPagerAdapter. It's getItem method can return a fragment according to data it has from the outside. After I update the data its suppose to display I call notifyDataSetChanged and yet nothing happened. I DID override the getItemPosition method to return POSITION_NONE:

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;     } ... 

And the update from outside is in a click event which sets the fragmentDisplayInfo var. I debuged and saw that the fragmentDisplayInfo is indeed initialized and the getItemPosition method does return POSITION_NONE, but the getItem method is not even called. Why is that?

EDIT:

If you hadn't done so as well, please notice that overiden part of the adapter:

 @Override public int getItemPosition(Object item) {     return POSITION_NONE; } 
like image 549
Yonatan Nir Avatar asked May 06 '15 14:05

Yonatan Nir


People also ask

How do I refresh Fragmentpageradapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.

How do I notify ViewPager?

The notifyDataSetChanged() method on the PagerAdapter will only notify the ViewPager that the underlying pages have changed. For example, if you have created/deleted pages dynamically (adding or removing items from your list) the ViewPager should take care of that.

What is the difference between PagerAdapter and Fragmentpageradapter?

The difference is that you can use Fragments inside a FragmentPageAdapter . If you want to have fragments that are going to be used in other parts of your code this is your Adapter. Otherwise if you only want to have code that isn't going to be reused, you can use PagerAdapter .


1 Answers

What Nik Myers is saying is correct. However there is a piece missing. When notifyDataSetChanged is called, the method getItemPosition is called. You need to override this to get the fragments to reload.

 @Override     public int getItemPosition(Object object) {         // Causes adapter to reload all Fragments when         // notifyDataSetChanged is called         return POSITION_NONE;     } 
like image 68
Skywalker Avatar answered Sep 21 '22 08:09

Skywalker