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