SHARE
TWEET

Untitled

a guest Feb 17th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MainActivity extends AppCompatActivity
  2. {
  3.     private static final long MOVE_DEFAULT_TIME = 1000;
  4.     private static final long FADE_DEFAULT_TIME = 300;
  5.  
  6.     private FragmentManager mFragmentManager;
  7.  
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState)
  10.     {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.  
  14.         ButterKnife.bind(this);
  15.         mFragmentManager = getSupportFragmentManager();
  16.  
  17.         loadInitialFragment();
  18.  
  19.         Handler handler = new Handler();
  20.         handler.postDelayed(this::performTransition, 1000);
  21.     }
  22.  
  23.     private void loadInitialFragment()
  24.     {
  25.         Fragment initialFragment = Fragment1.newInstance();
  26.         FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
  27.         fragmentTransaction.replace(R.id.fragment_container, initialFragment);
  28.         fragmentTransaction.commit();
  29.     }
  30.  
  31.     private void performTransition()
  32.     {
  33.         if (isDestroyed())
  34.         {
  35.             return;
  36.         }
  37.         Fragment previousFragment = mFragmentManager.findFragmentById(R.id.fragment_container);
  38.         Fragment nextFragment = Fragment2.newInstance();
  39.  
  40.         FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
  41.  
  42.         // 1. Exit for Previous Fragment
  43.         Fade exitFade = new Fade();
  44.         exitFade.setDuration(FADE_DEFAULT_TIME);
  45.         previousFragment.setExitTransition(exitFade);
  46.  
  47.         // 2. Shared Elements Transition
  48.         TransitionSet enterTransitionSet = new TransitionSet();
  49.         enterTransitionSet.addTransition(TransitionInflater.from(this).inflateTransition(android.R.transition.move));
  50.         enterTransitionSet.setDuration(MOVE_DEFAULT_TIME);
  51.         enterTransitionSet.setStartDelay(FADE_DEFAULT_TIME);
  52.         nextFragment.setSharedElementEnterTransition(enterTransitionSet);
  53.  
  54.         // 3. Enter Transition for New Fragment
  55.         Fade enterFade = new Fade();
  56.         enterFade.setStartDelay(MOVE_DEFAULT_TIME + FADE_DEFAULT_TIME);
  57.         enterFade.setDuration(FADE_DEFAULT_TIME);
  58.         nextFragment.setEnterTransition(enterFade);
  59.  
  60.         View logo = ButterKnife.findById(this, R.id.fragment1_logo);
  61.         fragmentTransaction.addSharedElement(logo, logo.getTransitionName());
  62.         fragmentTransaction.replace(R.id.fragment_container, nextFragment);
  63.         fragmentTransaction.commitAllowingStateLoss();
  64.     }
  65. }
RAW Paste Data
Top