0

I am using v4 Support fragment in my application with AppCompatActivity.

    loginUserFragment = new FragmentLoginUser();
    mFragmentTransaction = CommonUtils.getFragmentLRTransaction(this);
    mFragmentTransaction.add(R.id.fragmentContainer, loginUserFragment, loginUserFragment.getClass().getName());
    mFragmentTransaction.commit();

Fragment Left to Right Transaction:

public static FragmentTransaction getFragmentLRTransaction(FragmentActivity fa) {
    FragmentTransaction ft = fa.getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(anim.animated_activity_slide_left_in, anim.animated_activity_slide_right_out, anim.animated_activity_slide_right_in, anim.animated_activity_slide_left_out);
    return ft;
}

animated_activity_slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="-100%p"
android:toXDelta="0%p" 
android:duration="250"/>

animated_activity_slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%p"
android:toXDelta="100%p" 
android:duration="250"/>

animated_activity_slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="100%p"
android:toXDelta="0%p" 
android:duration="250"/>

animated_activity_slide_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%p"
android:toXDelta="-100%p" 
android:duration="250"/>

Neither Push nor Pop Animation is working :(

Krishnakant
  • 1,453
  • 3
  • 18
  • 30

3 Answers3

0

You can use translate to animate activity but you cannot be use translate to animate fragments. For fragments you you should use object animator.

UPDATE:

But like pointed out by @user3400729 support fragment manager cannot use object animator for transition but one can use translate to apply this animation.

In this case, i tried your problem out and found that the animation is working fine, all I did was increase the duration of the animation so that user can view this animation.

animated_activity_slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="-100%p"
android:toXDelta="0%p" 
android:duration="1500"/>

animated_activity_slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%p"
android:toXDelta="100%p" 
android:duration="1500"/>

animated_activity_slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="100%p"
android:toXDelta="0%p" 
android:duration="1500"/>

animated_activity_slide_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%p"
android:toXDelta="-100%p" 
android:duration="1500"/>

Hope it helps you..

Fabin Paul
  • 1,701
  • 1
  • 16
  • 18
0

replace mFragmentTransaction.add(...); with mFragmentTransaction.replace(...);

Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40
0

Try this,

Your push and pop fragments listed below,

public void pushFragments(String tag, Fragment fragment,
            boolean shouldAnimate, boolean shouldAdd) {
        try {
            if (shouldAdd)
                mStacks.get(tag).push(fragment);
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            if (shouldAnimate)
                ft.setCustomAnimations(R.anim.from_fade_in,
                        R.anim.from_fade_out);
            ft.replace(R.id.realTabContent, fragment);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pushFragments(String tag, Fragment fragment, Bundle args,
            boolean shouldAnimate, boolean shouldAdd) {
        try {
            if (shouldAdd)
                mStacks.get(tag).push(fragment).setArguments(args);
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            if (shouldAnimate)
                ft.setCustomAnimations(R.anim.from_fade_in,
                        R.anim.from_fade_out);
            ft.replace(R.id.realTabContent, fragment);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void popFragments() {
        try {
            Fragment fragment = mStacks
                    .get(GlobalFields.mCurrentTab)
                    .elementAt(mStacks.get(GlobalFields.mCurrentTab).size() - 2);

            /* pop current fragment from stack.. */
            mStacks.get(GlobalFields.mCurrentTab).pop();

            /*
             * We have the target fragment in hand.. Just show it.. Show a
             * standard navigation animation
             */
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            ft.setCustomAnimations(R.anim.to_fade_in, R.anim.to_fade_out);
            ft.replace(R.id.realTabContent, fragment);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

from_fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="100%"
        android:toXDelta="0" />

</set>

from_fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%" />

</set>

to_fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="-100%"
        android:toXDelta="0" />

</set>

to_fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%" />

</set>
Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35