FragmentTransaction animation to slide in over top

前端 未结 9 1371
星月不相逢
星月不相逢 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:49

    I found an alternate solution (not heavily tested) that I find more elegant than the proposals so far:

    final IncomingFragment newFrag = new IncomingFragment();
    newFrag.setEnterAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
    
                    }
    
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        clearSelection();
                        inFrag.clearEnterAnimationListener();
    
                        getFragmentManager().beginTransaction().remove(OutgoingFragment.this).commit();
                    }
    
                    @Override
                    public void onAnimationRepeat(Animation animation) {
    
                    }
                });
    
     getActivity().getSupportFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.slide_in_from_right, 0)
                        .add(R.id.container, inFrag)
                        .addToBackStack(null)
                        .commit();
    

    This is being called from within an inner class of the OutgoingFragment class.

    A new fragment is being inserted, the animation completes, then the old fragment is being removed.

    There may be some memory problems with this in some applications but it is better than retaining both fragments indefinitely.

提交回复
热议问题