Android pop fragment from backstack with animation

前端 未结 3 1136
天命终不由人
天命终不由人 2021-02-19 04:52

I have an activity that loads a Fragment A in it. Fragment A contains ListView and when clicking on list item I load another Fragment B in place of Fragment A to show the listvi

相关标签:
3条回答
  • 2021-02-19 05:00

    If you want backstack animations you'll need to use:

    setCustomAnimations(int enter, int exit, int popEnter, int popExit)

    Something like:

    setCustomAnimations(R.anim.enter_from_left, R.anim.enter_from_left,
            R.anim.exit_to_left, R.anim.exit_to_left); 
    
    0 讨论(0)
  • 2021-02-19 05:15

    With navigation component is very easy too:

            app:enterAnim="@anim/slide_in_left"
            app:exitAnim="@anim/slide_out_right"
            app:popEnterAnim="@anim/slide_in_right"
            app:popExitAnim="@anim/slide_out_left"
    
    0 讨论(0)
  • 2021-02-19 05:17

    In my case

    FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.setCustomAnimations(android.R.anim.slide_in_left, 
                                   android.R.anim.slide_out_right, 
                                   R.anim.slide_in_right,
                                   R.anim.slide_out_left);
            ft.replace(R.id.fragment_container, targetFragment).addToBackStack(null).commit();
    

    this line

    ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, 
                           R.anim.slide_in_right, R.anim.slide_out_left);
    

    would create perfect animation.

    slide_in_right

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="50%p" android:toXDelta="0"
                   android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
               android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    

    slide_out_left

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="0" android:toXDelta="-50%p"
                   android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
               android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    
    0 讨论(0)
提交回复
热议问题