0

I currently have one Activity which contains a Fragment by injecting it inside a LinearLayout during runtime.

Inside my Activity layout, I have a Button view called nextButton. When I click this button, I want the Activity to switch to the next Fragment but ALSO animate where the current fragment moves off the screen to the left and the new fragment comes in from the right. As if the new fragment is pushing the current fragment out of the way.

Below is a demonstration of what I want.

enter image description here

Shall I store all of the Fragments in an ArrayList<Fragment> and just inject the current index + 1 in to the LinearLayout when the nextButton is clicked? What would be the best way to go about this?

Subby
  • 4,819
  • 14
  • 66
  • 122

2 Answers2

1

All you need is ViewPager, See one good example here

You need to do selectPage with in your nextButton, here true is for smooth scroll animation.

pager.setCurrentItem( num,true )

Please find Reference link here

Community
  • 1
  • 1
Akhil
  • 6,495
  • 3
  • 27
  • 59
0

You need to create 4 animation for entering (left and right) and exiting animation (left and right) of the fragment.

Each time you replace a fragment from the FragmentTransaction you need to set the setCustomAnimations

sample:

FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(inL, inR, outL, outR);
    transaction.replace(layout, fragment, tag);
    transaction.commit();

where inL and inR are entering animation and outL and outR are exiting animation animation

Rod_Algonquin
  • 25,268
  • 6
  • 47
  • 61
  • That solves the easier part of the question. Where the more difficult side is to detect what current fragment is being shown and to which fragment shall it transition to. – Subby Jun 03 '14 at 21:11
  • @Subby to detect the current fragment you can just check the TAG as you could see every time you replace a fragment you put a tag on it – Rod_Algonquin Jun 03 '14 at 21:17
  • "which fragment shall it transition to" that depends on your collection of fragments – Rod_Algonquin Jun 03 '14 at 21:18