0

I need to reload action and refresh the fragment every time it switches inside FragmentPagerAdapter I have seen references from

ViewPager PagerAdapter not updating the View

How to refresh a Fragment of a FragmentPagerAdapter?

My FragmentPagerAdapter

public class SickAdapterApproval extends FragmentPagerAdapter{

    private static final int FRAGMENT_COUNT = 4;

    public SickAdapterApproval(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new SickFragmentToAll();
            case 1:
                return new SickFragmentToPending();
            case 2:
                return new SickFragmentToApproved();
            case 3:
                return new SickFragmentToDenied();
        }
        return null;
    }

    @Override
    public int getCount() {
        return FRAGMENT_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return "All";
            case 1:
                return "Pending";
            case 2:
                return "Approved";
            case 3:
                return "Denied";
        }
        return null;
    }
}

but I still stuck and do not understand, is there someone who can explain it back to me and give an example?

  • Do you just want to reload an already existing item of the PagerAdapter or do you want the PagerAdapter to refresh after you messed with the number of items inside the adapter (add or remove)? – kalabalik Sep 20 '17 at 07:48
  • @KalaBalik I want to refresh the fragment because the content of the fragment loads the data from json and I want every time I replace the fragment, the fragment is reloaded in order to get the latest data from json –  Sep 20 '17 at 07:52

2 Answers2

1

change FragmentPageAdapter with `FragmentStatePageAdapater

and Override this method:

@Override
public int getItemPosition(Object object) {
//FragmentLeft is the class for the first fragment in the view
//recreate only FragmentLeft
    if (object instanceof FragmentLeft) {
        return POSITION_NONE;
    }
    return 1;
}
chandrakant sharma
  • 1,290
  • 9
  • 14
  • It's work, just change ``FragmentPageAdapter`` with ``FragmentStatePageAdapter`` on Fragment Class, but what's different about **FragmentPageAdapter** and **FragmentStatePageAdapter**? –  Sep 20 '17 at 07:55
  • But what's different about `FragmentPageAdapter` and `FragmentStatePageAdapter` –  Sep 20 '17 at 07:58
  • When notifyDataSetChanged() is called, the adapter calls the notifyChanged() method of the ViewPager which it is attached to. The ViewPager then checks the value returned by the adapter's getItemPosition() for each item, removing those items which return POSITION_NONE (see the source code) and then repopulating. – chandrakant sharma Sep 20 '17 at 07:59
  • but I just replace **FragmentPageAdapter** to **FragmentStatePageAdapter** and it's work, I didn't add getItemPosition, how is that ? –  Sep 20 '17 at 08:04
  • may you please mark it as accepted answer you can can read the difference on Android developers guide – chandrakant sharma Sep 20 '17 at 08:06
0

If you only want to refresh the content of an existing fragment, neither notifyDataSetChanged() nor the adapter is involved in the solution. You need to make sure that the view you are feeding with your data gets refreshed. See my source:

The notifyDatasetChanged() method is not meant to be used for refreshing the current displayed Fragments or their views. You need to add some listeners / callbacks to your Fragments if you want them to refresh their views if some of your data changes.

kalabalik
  • 3,465
  • 2
  • 18
  • 42
  • but I just replace **FragmentPageAdapter** to **FragmentStatePageAdapter** and it's work, I didn't add getItemPosition, how is that ? –  Sep 20 '17 at 08:04
  • You should check if the fragment is recreated or not (put a log inside the onCreate() method of the fragment, for example). If not you are fine. – kalabalik Sep 20 '17 at 08:16