1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.support.transition.widget; 18 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 import androidx.core.view.ViewCompat; 25 import androidx.fragment.app.Fragment; 26 import androidx.fragment.app.FragmentManager; 27 import androidx.interpolator.view.animation.FastOutSlowInInterpolator; 28 import androidx.transition.AutoTransition; 29 import androidx.transition.Fade; 30 import androidx.transition.Transition; 31 32 import com.example.android.support.transition.R; 33 34 import org.jspecify.annotations.Nullable; 35 36 /** 37 * Demonstrates usage of shared element Transition between Fragments. 38 */ 39 public class FragmentTransitionUsage extends TransitionUsageBase { 40 41 private static final String SHARED = "red"; 42 43 private static final Transition SHARED_TRANSITION = new AutoTransition(); 44 private static final Transition NON_SHARED_TRANSITION = new Fade(); 45 46 static { 47 SHARED_TRANSITION.setDuration(1000); SHARED_TRANSITION.setInterpolator(new FastOutSlowInInterpolator())48 SHARED_TRANSITION.setInterpolator(new FastOutSlowInInterpolator()); 49 NON_SHARED_TRANSITION.setDuration(1000); NON_SHARED_TRANSITION.setInterpolator(new FastOutSlowInInterpolator())50 NON_SHARED_TRANSITION.setInterpolator(new FastOutSlowInInterpolator()); 51 } 52 53 private static final String FRAGMENT_FIRST = "first"; 54 private static final String FRAGMENT_SECOND = "second"; 55 56 @Override getLayoutResId()57 int getLayoutResId() { 58 return R.layout.fragment_transition; 59 } 60 61 @Override onCreate(Bundle savedInstanceState)62 protected void onCreate(Bundle savedInstanceState) { 63 super.onCreate(savedInstanceState); 64 if (savedInstanceState == null) { 65 getSupportFragmentManager().beginTransaction() 66 .replace(R.id.container, new FirstFragment(), FRAGMENT_FIRST) 67 .setReorderingAllowed(true) 68 .commitNow(); 69 } 70 } 71 showSecond(View sharedElement)72 void showSecond(View sharedElement) { 73 FragmentManager fragmentManager = getSupportFragmentManager(); 74 final FirstFragment first = 75 (FirstFragment) fragmentManager.findFragmentByTag(FRAGMENT_FIRST); 76 if (first == null) { 77 return; 78 } 79 final SecondFragment second = new SecondFragment(); 80 81 fragmentManager.beginTransaction() 82 .replace(R.id.container, second, FRAGMENT_SECOND) 83 .addToBackStack(null) 84 .setReorderingAllowed(true) 85 .addSharedElement(sharedElement, SHARED) 86 .commit(); 87 } 88 89 private abstract static class TransitionFragment extends Fragment { 90 91 @Override onActivityCreated(@ullable Bundle savedInstanceState)92 public void onActivityCreated(@Nullable Bundle savedInstanceState) { 93 super.onActivityCreated(savedInstanceState); 94 setSharedElementEnterTransition(SHARED_TRANSITION); 95 setSharedElementReturnTransition(SHARED_TRANSITION); 96 setExitTransition(NON_SHARED_TRANSITION); 97 setEnterTransition(NON_SHARED_TRANSITION); 98 setReenterTransition(NON_SHARED_TRANSITION); 99 setReturnTransition(NON_SHARED_TRANSITION); 100 setAllowEnterTransitionOverlap(true); 101 setAllowReturnTransitionOverlap(true); 102 } 103 104 } 105 106 /** 107 * A {@link Fragment} with red and yellow squares. 108 */ 109 public static class FirstFragment extends TransitionFragment { 110 111 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)112 public @Nullable View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 113 @Nullable Bundle savedInstanceState) { 114 return inflater.inflate(R.layout.fragment_transition_first, container, false); 115 } 116 117 @Override onViewCreated(View view, @Nullable Bundle savedInstanceState)118 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 119 final View red = view.findViewById(R.id.red); 120 ViewCompat.setTransitionName(red, SHARED); 121 view.findViewById(R.id.move).setOnClickListener(new View.OnClickListener() { 122 @Override 123 public void onClick(View v) { 124 FragmentTransitionUsage activity = (FragmentTransitionUsage) getActivity(); 125 if (activity != null) { 126 activity.showSecond(red); 127 } 128 } 129 }); 130 } 131 132 } 133 134 /** 135 * A {@link Fragment} with red and blue squares. 136 */ 137 public static class SecondFragment extends TransitionFragment { 138 139 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)140 public @Nullable View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 141 @Nullable Bundle savedInstanceState) { 142 return inflater.inflate(R.layout.fragment_transition_second, container, false); 143 } 144 145 @Override onViewCreated(View view, @Nullable Bundle savedInstanceState)146 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 147 ViewCompat.setTransitionName(view.findViewById(R.id.red), SHARED); 148 } 149 150 } 151 152 } 153