-2

I have a ViewPager and its child fragments are dynamic. I am using them in the same fragment class and I am changing the field's value dynamically. But when I changing the ViewPager position, It is not updating the fragment. I have to change the values instantly.

Thank you.

Aj 27
  • 2,316
  • 21
  • 29
Afinas EM
  • 2,755
  • 1
  • 15
  • 28
  • If you want to recreate fragments on every swipe, than in your viewPager you can add this code ---- mPage.setOffscreenPageLimit(0); – Hayk Mkrtchyan Mar 13 '18 at 09:02
  • 1
    Possible duplicate of [Update Fragment from ViewPager](https://stackoverflow.com/questions/18088076/update-fragment-from-viewpager) – Keivan Esbati Mar 13 '18 at 09:04

2 Answers2

0

Declare refreshFragment method in your fragment class then

In ViewPagerAdapter class overwritte method

@Override
    public int getItemPosition(@NonNull Object object) {
       MyFragment f = (MyFragment ) object;
        if (f != null) {
           f.refreshFragment();
        }
        return super.getItemPosition(object);
    }

When you call

viewPagerAdapter.notifyDataSetChanged();

it will call getItemPosition method in adapter class and update your fragment using refreshFragment method.

MJM
  • 5,119
  • 5
  • 27
  • 53
0

Android by default retains one page on both sides of the current page for optimised loading. Since you need the pages to refresh on every time, you need to use viewpager.setOffScreenPageLimit(0) to set all pages to be recreated every time they are viewed

Anuraag Baishya
  • 874
  • 2
  • 11
  • 26