I\'m migrating my ViewPager
to ViewPager2
since the latter is supposed to solve all the problems of the former. Unfortunately, when using it with a
I also had your problem and used this trick
Map map = new HashMap<>();
@Override
public Fragment createFragment(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new CircularFragment();
map.put(0, fragment);
return fragment;
case 1:
fragment = new LocalFragment();
map.put(1, fragment);
return fragment;
case 2:
fragment = new SettingsFragment();
map.put(2, fragment);
return fragment;
}
fragment = new CircularFragment();
map.put(0, fragment);
return fragment;
}
and you can get fragment like this
LocalFragment f = (LocalFragment) map.get(1);