FragmentTransaction animation to slide in over top

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

    Add an elevation to the layout.

    I added an elevation to the fragment that slides in from the bottom of 30sp and it worked.

    I have tried many solutions suggested here. Here are the full code and output that combines all the idea + adding elevation.

    Output:

    Without elevation:

    With elevation:

    Full Code:

    Add an elevation to the root

    
         ----
    
         ----
     
    

    How to slide in the fragment from the bottom?

    getSupportFragmentManager()
                .beginTransaction()
                .setCustomAnimations(R.anim.slide_in_bottom, R.anim.do_nothing, R.anim.do_nothing, R.anim.slide_out_bottom)
                .replace(R.id.fragmentContainer, currentFragment, "TAG")
                .addToBackStack("TAG")
                .commit();
    

    How to do the reverse when the back button is pressed?

    getSupportFragmentManager()
                .popBackStack();
    

    Since we have already defined enter and exit animation on setCustomAnimations() method. Calling popBackStack(); takes care of the reverse animation.

    R.anim.slide_in_bottom

    
        
        
    
    

    R.anim.slide_out_bottom

    
        
        
    
    

    R.anim.do_nothing

    
        
         
    
    

提交回复
热议问题