支持片段推送流行动画

时间:2015-10-27 05:45:58

标签: android android-fragments android-animation android-transitions fragmentmanager

我在AppCompatActivity的应用程序中使用v4支持片段。

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

片段从左到右的交易:

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"/>

Push和Pop Animation都没有工作:(

3 个答案:

答案 0 :(得分:0)

您可以使用translate来为活动设置动画,但不能使用translate来设置片段的动画。对于片段,您应该使用对象动画。

更新:

但是像@ user3400729指出的支持片段管理器不能使用对象动画师进行转换,但可以使用translate来应用这个动画。

在这种情况下,我尝试了你的问题,发现动画工作正常,我所做的就是增加动画的持续时间,以便用户可以观看这个动画。

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"/>

希望它可以帮助你..

答案 1 :(得分:0)

mFragmentTransaction.add(...);替换为mFragmentTransaction.replace(...);

答案 2 :(得分:0)

试试这个,

下面列出的推送和流行片段

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>