FragmentTransaction animation to slide in over top
I found a solution which works for me. I ended up using a ViewPager with a FragmentStatePagerAdapter. The ViewPager provides the swiping behavior and the FragmentStatePagerAdapter swaps in the fragments. The final trick to achieve the effect of having one page visible "under" the incoming page is to use a PageTransformer. The PageTransformer overrides the ViewPager's default transition between pages. Here is an example PageTransformer that achieves the effect with translation and a small amount of scaling on the left-hand side page.
public class ScalePageTransformer implements PageTransformer {
private static final float SCALE_FACTOR = 0.95f;
private final ViewPager mViewPager;
public ScalePageTransformer(ViewPager viewPager) {
this.mViewPager = viewPager;
}
@SuppressLint("NewApi")
@Override
public void transformPage(View page, float position) {
if (position <= 0) {
// apply zoom effect and offset translation only for pages to
// the left
final float transformValue = Math.abs(Math.abs(position) - 1) * (1.0f - SCALE_FACTOR) + SCALE_FACTOR;
int pageWidth = mViewPager.getWidth();
final float translateValue = position * -pageWidth;
page.setScaleX(transformValue);
page.setScaleY(transformValue);
if (translateValue > -pageWidth) {
page.setTranslationX(translateValue);
} else {
page.setTranslationX(0);
}
}
}
}
Fragments and slide over animation
After quite a bit of experimentation and testing, here is the method I adopted.
Probably not the best, but it works for me.
- Create an animator for fragments which does nothing
- When creating the new fragment, pass in the initial X position, which is the negative width of the container view. This will put the new fragment offset to the left of the container view and therefor hidden, for now.
- Call setCustomAnimations on the FragmentTransaction with the "stay still" animation
- Using the FragmentTransaction add the new fragment
- Create an animation listener which, in onAnimationEnd, will call the containing view to do the following.
- Swap the order of the two views by calling bringChildToFront
- Start a new view animation on the old view to shrink it and fade it out
- Start a new view animation on the new view to slide it in
- Attach an AnimatorListener to the animation so that the old view can be removed at the animation end.
These are the steps to follow, the rest is just implementation :-D
Related Topics
Android in Navigation Drawer When I Click Anywhere Drawer Closes
Parsing Date/Time to Localtimezone
Firebase Servervalue.Timestamp in Java Data Models Objects
How to Have a Masked Numeric Input Field
Borders on Certain Zoom Levels on Webpage
How to Add Network_Security_Config.Xml to Manifest in Expo App Without Ejecting
Call Recorder Not Working in Android 10 (Q)
Android Listview Child View Setenabled() and Setclickable() Do Nothing
Android Stream Video from Google Drive
Gradle Dsl Method Not Found: 'Implementation()'
Overriding the Power Button in Android
How to Secure My App Against Piracy
When Should I Recycle a Bitmap Using Lrucache
Android Collapsingtoolbarlayout Collapse Listener
How to Handle Asynctask Onpostexecute When Paused to Avoid Illegalstateexception