FragmentTransaction animation to slide in over top

前端 未结 9 1368
星月不相逢
星月不相逢 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 20:58

    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);
                }
            }
        }
    
    }
    

提交回复
热议问题