FragmentTransaction animation to slide in over top

前端 未结 9 1390
星月不相逢
星月不相逢 2020-11-30 20:26

I am trying to achieve the following effect using FragmentTransaction.setCustomAnimations.

  1. Fragment A is showing
  2. Replace Fragment A with Fragment B. F
9条回答
  •  天涯浪人
    2020-11-30 21:00

    Update (June 16, 2020)

    Starting from fragment library 1.2.0 the recommanded way to fix this issue is to use FragmentContainerView with FragmentTransaction.setCustomAnimations().

    According to the documentation:

    Fragments using exit animations are drawn before all others for FragmentContainerView. This ensures that exiting Fragments do not appear on top of the view.

    Steps to fix this issue are:

    1. Update fragment library to 1.2.0 or more androidx.fragment:fragment:1.2.0;
    2. Replace your xml fragment container (, , or else) by ;
    3. Use FragmentTransaction.setCustomAnimations() to animate your fragments transitions.

    Previous answer (Nov 19, 2015)

    Starting from Lollipop, you can increase de translationZ of your entering fragment. It will appear above the exiting one.

    For example:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewCompat.setTranslationZ(getView(), 100.f);
    }
    

    If you want to modify the translationZ value only for the duration of the animation, you should do something like this:

    @Override
    public Animation onCreateAnimation(int transit, final boolean enter, int nextAnim) {
        Animation nextAnimation = AnimationUtils.loadAnimation(getContext(), nextAnim);
        nextAnimation.setAnimationListener(new Animation.AnimationListener() {
    
            private float mOldTranslationZ;
    
            @Override
            public void onAnimationStart(Animation animation) {
                if (getView() != null && enter) {
                    mOldTranslationZ = ViewCompat.getTranslationZ(getView());
                    ViewCompat.setTranslationZ(getView(), 100.f);
                }
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                if (getView() != null && enter) {
                    ViewCompat.setTranslationZ(getView(), mOldTranslationZ);
                }
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        return nextAnimation;
    }
    

提交回复
热议问题