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.
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?
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
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
Related
I have 2 fragments the first contains a button which on clicked opens a fragment with a ListView in it. I have a shared element transition for the button to transition into the new fragment (root layout) but I would also like to have this transition in reverse (the list fragment contracts into the button again).
However currently I detect the list item click and send an event to the Activity which pops the listview fragment off the backstack (popBackStackImmediate()) hence does not show the transition.
Is there a good way to allow back navigation while preserving reverse transition to work as well?
did you try this:
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
fm.executePendingTransactions();
PS: use getSupportFragmentManager() if you're using the support library else use getFragmentManager().
I am try to do two animation while navigation from one fragment to other fragment.
When I am in onefragment,On the tab click of activity,current fragment should slide left and on the launch of new fragment it should be sliding from right.
I tried intially using FragmentTransaction's setCustomAnimations(),but it was not successful.
So I am doing animations on status of fragment.So the SlideLeft transititionm,I am doing in Stop() of the fragment,While SlideIn transistion has been done on OnAcitvityCreated() of the fragment.
Please let me know Is this correct way or any other appropriate way to handle this
I have two activities when I click the Button of the First activity it should move to right not completely out.At the same time the second activity should move from left to right this activity will occupy the 50% of the screen and rest of the Screen will be the first activity.The button will be in first activity .If I again click the button of the first activity the second activity will move from right to left it disappears and the screen is occupied by first activity. I done using override Pending Transition in android the problem is when I click the button the first activity is slide out and second activity occupies screen
You can't show half of activity at a time there will be only one activity visible to user and for implementing the animation you mentioned above you can use two fragment in a activity.
Use two fragment and use animation while adding and removing them.
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MyFragment next = getMyFragment();
ft.add(R.id.MyLayout,next);
ft.setCustomAnimations(R.anim.slide_in_right,0);
ft.show(next);
ft.commit();
My problem
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose ViewPager has 3 fragments to display: Fragment1, Fragment2, Fragment3. Fragment1 is a grid fragment and displays a grid. Fragment2 and Fragment3 have their own content to display.
When the use swipes on the screen, ViewPager will display the next fragment - Fragment2. And so on.
What I want?
What I want is that, when an item from the grid (displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). User will work on Fragment4 and after ButtonBack click (just button iside Fragment4), Fragment1 with the grid should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, the next fragment (in our case Fragment2) will be displayed.
So I just want to get the same behavior as in example:
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
My question
So, is this possible to achieve? If yes, then how to?
I would greatly appreciate for your help. Alex.
P.S. Sorry for my English:)
I've made a little example that shows how to achieve it:
https://github.com/danilao/fragments-viewpager-example
I think the point is to use another fragment as a container.
One way to achieve this is by adding another FragmentActivity that displays your Fragment4. Use Intent to start this activity and sent grid item position or other data in Extra.
On press of backbutton this activity will be finished and last activity with view pager will be displayed.
[EDIT] Old response. Better solutions are provided in other answers,
You will have to do the fragment transaction and add the existing fragment into backstack.
I created a sample project to show you how to do it.
The code is not fine tuned, I just created it to show you how to do it.
https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing
But please be aware that even if you press back at any of the view pager screen, the previous fragment ends up showing as it is the same activity.
IF this is not expected behavior, then you should consider having multiple activities instead ( like tab layout )
I'm trying to add animation to my android app, and knew a lot about transition animation between activities, and about fragments. So I have MainActivity with some view (image), and SecondActivity which is empty itself, but contains fragments, which then needs to be used to display some detail information about this image. I've read about postponeentertransition() and it worked. But the problem is, that this fragment in second activity have links(or transitions) to the same kind of fragment.
Let's say in main activity I have set of images of cars, then I click one, second activity is starting and waiting for fragment loading, and then shows animation. And then, in this fragment I have "similar cars" recycler, and choose one of them, and wants, to show its details the same way, but with adding this fragment to the backstack (not replacing). So the problem is that transition name of the view in main activity and big image of fragment are the same (which is obvious), and transition name in fragment's recycler and big image of the next fragment have to be the same too(otherwise app doesn't know how to make the animation). On the other side, fragment (which was added) has to be the same class, but its different instance.
I just want to set up animation without crashing the existing architecture.:) If you have any solutions or advices how to make animation between different instances of one fragment, please help.)
Use setCustomAnimations on the Fragment transaction (see below). It doesn't matter if the instances are from the same class.
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
// To animate the fragment
// NOTE: Must be before replace/add otherwise it doesn't work
transaction
.setCustomAnimations(enter, exit, popEnter, popExit)
.replace(R.id.frame_container, fragment, null)
.commit();