Closed emanzanoaxa closed 6 years ago
Surely. You can implement your own ScreenSwitcher
based on a view pager.
Can you give me some hints? I've been checking FragmentScreenSwitcher and I can't realize how to mix it with a FragmentPagerAdapter. The solution I'm thinking of is to get the fragments from the NavigationFactory when the FragmentPagerAdapter asks for them, but I don't know if then the fragments will be always kept on memory even if they are several pages.
EDIT: this seems to do the work
/**
* Fragment state pager adapter that gets the fragments from NavigationFactory, the fragments
* to be loaded are defined in a Screen list.
*/
public class ScreenFragmentPagerAdater extends FragmentPagerAdapter {
protected final List<Screen> screens;
private final NavigationFactory navigationFactory;
private final Map<Screen, WeakReference<Fragment>> fragmentMap;
public ScreenFragmentPagerAdater(FragmentManager fm, NavigationFactory navigationFactory) {
super(fm);
Preconditions.checkNotNull(navigationFactory);
this.navigationFactory = navigationFactory;
fragmentMap = new HashMap<>();
this.screens = new ArrayList<>();
}
public void setScreens(@NonNull List<Screen> screens) {
Preconditions.checkNotNull(screens);
this.screens.clear();
this.screens.addAll(screens);
notifyDataSetChanged();
}
@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
fragmentMap.put(screens.get(position), new WeakReference<>(fragment));
return fragment;
}
@Override
public Fragment getItem(int position) {
try {
return getOrCreateFragment(screens.get(position));
} catch (ScreenSwitchingException e) {
throw new RuntimeException(e);
}
}
@Override
public int getCount() {
return screens.size();
}
private Fragment getOrCreateFragment(Screen screen) throws ScreenSwitchingException {
Fragment fragment = fragmentMap.get(screen) != null ? fragmentMap.get(screen).get() : null;
if (fragment == null) {
fragment = navigationFactory.createFragment(screen);
try {
navigationFactory.getScreen(fragment); // Check that the screen has a valid screen getting function
} catch (Exception e) {
throw new ScreenSwitchingException(e.getMessage());
}
fragmentMap.put(screen, new WeakReference<>(fragment));
}
return fragment;
}
}
The solution I'm thinking of is to get the fragments from the NavigationFactory when the FragmentPagerAdapter asks for them.
Yes, it should work.
but I don't know if then the fragments will be always kept on memory even if they are several pages.
NavigationFactory
just creates fragments and does not hold references to them anymore. Then FragmentPagerAdapter will handle this fragments as it's supposed to do. According to the documentation FragmentPagerAdapter keeps all created fragments in the fragment manager. It is okay if you have a small amount of pages. In other case you can try FragmentStatePagerAdapter.
Is there a way to use switchTo() or other command on a fragment view pager? Or a way to imitate the ViewPager behavior using animations without losing the swipe functionality?