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 17package com.example.android.support.transition.widget; 18 19import android.os.Bundle; 20import android.support.annotation.Nullable; 21import android.support.transition.AutoTransition; 22import android.support.transition.Fade; 23import android.support.transition.Transition; 24import android.support.v4.app.Fragment; 25import android.support.v4.app.FragmentManager; 26import android.support.v4.view.ViewCompat; 27import android.support.v4.view.animation.FastOutSlowInInterpolator; 28import android.view.LayoutInflater; 29import android.view.View; 30import android.view.ViewGroup; 31 32import com.example.android.support.transition.R; 33 34/** 35 * Demonstrates usage of shared element Transition between Fragments. 36 */ 37public class FragmentTransitionUsage extends TransitionUsageBase { 38 39 private static final String SHARED = "red"; 40 41 private static final Transition SHARED_TRANSITION = new AutoTransition(); 42 private static final Transition NON_SHARED_TRANSITION = new Fade(); 43 44 static { 45 SHARED_TRANSITION.setDuration(1000); 46 SHARED_TRANSITION.setInterpolator(new FastOutSlowInInterpolator()); 47 NON_SHARED_TRANSITION.setDuration(1000); 48 NON_SHARED_TRANSITION.setInterpolator(new FastOutSlowInInterpolator()); 49 } 50 51 private static final String FRAGMENT_FIRST = "first"; 52 private static final String FRAGMENT_SECOND = "second"; 53 54 @Override 55 int getLayoutResId() { 56 return R.layout.fragment_transition; 57 } 58 59 @Override 60 protected void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 if (savedInstanceState == null) { 63 getSupportFragmentManager().beginTransaction() 64 .replace(R.id.container, new FirstFragment(), FRAGMENT_FIRST) 65 .setReorderingAllowed(true) 66 .commitNow(); 67 } 68 } 69 70 void showSecond(View sharedElement) { 71 FragmentManager fragmentManager = getSupportFragmentManager(); 72 final FirstFragment first = 73 (FirstFragment) fragmentManager.findFragmentByTag(FRAGMENT_FIRST); 74 if (first == null) { 75 return; 76 } 77 final SecondFragment second = new SecondFragment(); 78 79 fragmentManager.beginTransaction() 80 .replace(R.id.container, second, FRAGMENT_SECOND) 81 .addToBackStack(null) 82 .setReorderingAllowed(true) 83 .addSharedElement(sharedElement, SHARED) 84 .commit(); 85 } 86 87 private abstract static class TransitionFragment extends Fragment { 88 89 @Override 90 public void onActivityCreated(@Nullable Bundle savedInstanceState) { 91 super.onActivityCreated(savedInstanceState); 92 setSharedElementEnterTransition(SHARED_TRANSITION); 93 setSharedElementReturnTransition(SHARED_TRANSITION); 94 setExitTransition(NON_SHARED_TRANSITION); 95 setEnterTransition(NON_SHARED_TRANSITION); 96 setReenterTransition(NON_SHARED_TRANSITION); 97 setReturnTransition(NON_SHARED_TRANSITION); 98 setAllowEnterTransitionOverlap(true); 99 setAllowReturnTransitionOverlap(true); 100 } 101 102 } 103 104 /** 105 * A {@link Fragment} with red and yellow squares. 106 */ 107 public static class FirstFragment extends TransitionFragment { 108 109 @Nullable 110 @Override 111 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 112 @Nullable Bundle savedInstanceState) { 113 return inflater.inflate(R.layout.fragment_transition_first, container, false); 114 } 115 116 @Override 117 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 118 final View red = view.findViewById(R.id.red); 119 ViewCompat.setTransitionName(red, SHARED); 120 view.findViewById(R.id.move).setOnClickListener(new View.OnClickListener() { 121 @Override 122 public void onClick(View v) { 123 FragmentTransitionUsage activity = (FragmentTransitionUsage) getActivity(); 124 if (activity != null) { 125 activity.showSecond(red); 126 } 127 } 128 }); 129 } 130 131 } 132 133 /** 134 * A {@link Fragment} with red and blue squares. 135 */ 136 public static class SecondFragment extends TransitionFragment { 137 138 @Nullable 139 @Override 140 public 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 146 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 147 ViewCompat.setTransitionName(view.findViewById(R.id.red), SHARED); 148 } 149 150 } 151 152} 153