Search code examples
androidandroid-fragmentsandroid-viewpager

ViewPager not for all activity fragments


So I saw on an android developer site the example implementation of a ViewPager. I saw that ViewPager must be set in an activity class. My application has only one Actvity with about 15 Fragments. The thing I want to do is to use ViewPager only for 3 Fragments, for example:

  • I go to Fragment 5
  • When I click back I go to Fragment 1 (this is yet implemented)
  • When I swipe in Fragment 5 from left to right I go to Fragment 4
  • When I swipe in Fragment 5 from right to left I go to Fragment 6
  • When I swipe in the Fragment4/Fragment6 I go back to Fragment 5
  • When I swipe in the rest of Fragments, nothing happens

Is this even possible with only one Activity containing these all fragments? I will be grateful for any kind of hints.

PS: If that is gonna be helpful answering my question, this is how I switch between fragments now:

MealInfoFragment fragment = new MealInfoFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(MainActivity.currentFragment);
fragmentTransaction.commit();

Solution

  • Just for the record, I did it by creating a viewpager in a fragment that contains other 3 fragments!