18
18
19
19
import io .material .catalog .R ;
20
20
21
+ import android .app .ActivityOptions ;
21
22
import android .content .Context ;
22
23
import android .content .Intent ;
23
24
import android .content .res .ColorStateList ;
24
25
import android .content .res .TypedArray ;
26
+ import android .os .Build .VERSION ;
27
+ import android .os .Build .VERSION_CODES ;
25
28
import android .os .Bundle ;
26
29
import androidx .annotation .ArrayRes ;
27
30
import androidx .annotation .ColorInt ;
28
31
import androidx .annotation .DimenRes ;
29
32
import androidx .annotation .Nullable ;
30
33
import androidx .annotation .StringRes ;
31
34
import androidx .fragment .app .Fragment ;
35
+ import androidx .fragment .app .FragmentActivity ;
32
36
import androidx .core .view .MarginLayoutParamsCompat ;
33
37
import androidx .core .view .MenuItemCompat ;
38
+ import androidx .core .view .ViewCompat ;
34
39
import androidx .appcompat .app .AppCompatActivity ;
35
40
import androidx .appcompat .widget .Toolbar ;
36
41
import android .view .LayoutInflater ;
42
47
import android .view .ViewGroup .MarginLayoutParams ;
43
48
import android .widget .TextView ;
44
49
import com .google .android .material .resources .MaterialResources ;
50
+ import com .google .android .material .transition .MaterialContainerTransformSharedElementCallback ;
45
51
import dagger .android .support .DaggerFragment ;
46
52
import java .util .Collections ;
47
53
import java .util .List ;
@@ -68,6 +74,12 @@ public View onCreateView(
68
74
layoutInflater .inflate (
69
75
R .layout .cat_demo_landing_fragment , viewGroup , false /* attachToRoot */ );
70
76
77
+ Bundle arguments = getArguments ();
78
+ if (arguments != null ) {
79
+ String transitionName = arguments .getString (FeatureDemoUtils .ARG_TRANSITION_NAME );
80
+ ViewCompat .setTransitionName (view , transitionName );
81
+ }
82
+
71
83
Toolbar toolbar = view .findViewById (R .id .toolbar );
72
84
73
85
AppCompatActivity activity = (AppCompatActivity ) getActivity ();
@@ -145,7 +157,9 @@ private void addDemoView(
145
157
TextView titleTextView = demoView .findViewById (R .id .cat_demo_landing_row_title );
146
158
TextView subtitleTextView = demoView .findViewById (R .id .cat_demo_landing_row_subtitle );
147
159
148
- rootView .setOnClickListener (v -> startDemo (demo ));
160
+ String transitionName = getString (demo .getTitleResId ());
161
+ ViewCompat .setTransitionName (rootView , transitionName );
162
+ rootView .setOnClickListener (v -> startDemo (v , demo , transitionName ));
149
163
150
164
titleTextView .setText (demo .getTitleResId ());
151
165
subtitleTextView .setText (getDemoClassName (demo ));
@@ -169,26 +183,41 @@ private String getDemoClassName(Demo demo) {
169
183
}
170
184
}
171
185
172
- private void startDemo (Demo demo ) {
186
+ private void startDemo (View sharedElement , Demo demo , String transitionName ) {
173
187
if (demo .createFragment () != null ) {
174
- startDemoFragment (demo .createFragment ());
188
+ startDemoFragment (sharedElement , demo .createFragment (), transitionName );
175
189
} else if (demo .createActivityIntent () != null ) {
176
- startDemoActivity (demo .createActivityIntent ());
190
+ startDemoActivity (sharedElement , demo .createActivityIntent (), transitionName );
177
191
} else {
178
192
throw new IllegalStateException ("Demo must implement createFragment or createActivityIntent" );
179
193
}
180
194
}
181
195
182
- private void startDemoFragment (Fragment fragment ) {
196
+ private void startDemoFragment (View sharedElement , Fragment fragment , String transitionName ) {
183
197
Bundle args = new Bundle ();
184
198
args .putString (DemoFragment .ARG_DEMO_TITLE , getString (getTitleResId ()));
199
+ args .putString (FeatureDemoUtils .ARG_TRANSITION_NAME , transitionName );
185
200
fragment .setArguments (args );
186
- FeatureDemoUtils .startFragment (getActivity (), fragment , FRAGMENT_DEMO_CONTENT );
201
+ FeatureDemoUtils .startFragment (
202
+ getActivity (), fragment , FRAGMENT_DEMO_CONTENT , sharedElement , transitionName );
187
203
}
188
204
189
- private void startDemoActivity (Intent intent ) {
205
+ private void startDemoActivity (View sharedElement , Intent intent , String transitionName ) {
190
206
intent .putExtra (DemoActivity .EXTRA_DEMO_TITLE , getString (getTitleResId ()));
191
- startActivity (intent );
207
+ intent .putExtra (DemoActivity .EXTRA_TRANSITION_NAME , transitionName );
208
+
209
+ if (VERSION .SDK_INT >= VERSION_CODES .LOLLIPOP ) {
210
+ // Set up shared element transition and disable overlay so views don't show above system bars
211
+ FragmentActivity activity = getActivity ();
212
+ activity .setExitSharedElementCallback (new MaterialContainerTransformSharedElementCallback ());
213
+ activity .getWindow ().setSharedElementsUseOverlay (false );
214
+
215
+ ActivityOptions options =
216
+ ActivityOptions .makeSceneTransitionAnimation (activity , sharedElement , transitionName );
217
+ startActivity (intent , options .toBundle ());
218
+ } else {
219
+ startActivity (intent );
220
+ }
192
221
}
193
222
194
223
private void setMarginStart (View view , @ DimenRes int marginResId ) {
@@ -231,9 +260,9 @@ private void setMenuItemChecked(MenuItem menuItem, boolean isChecked) {
231
260
/**
232
261
* Whether or not the feature shown by this fragment should be flagged as restricted.
233
262
*
234
- * Examples of restricted feature could be features which depends on an API level that is
235
- * greater than MDCs min sdk version. If overriding this method, you should also override
236
- * {@link #getRestrictedMessageId()} and provide information about why the feature is restricted.
263
+ * <p> Examples of restricted feature could be features which depends on an API level that is
264
+ * greater than MDCs min sdk version. If overriding this method, you should also override {@link
265
+ * #getRestrictedMessageId()} and provide information about why the feature is restricted.
237
266
*/
238
267
public boolean isRestricted () {
239
268
return false ;
@@ -242,10 +271,10 @@ public boolean isRestricted() {
242
271
/**
243
272
* The message to display if a feature {@link #isRestricted()}.
244
273
*
245
- * This message should provide insight into why the feature is restricted for the device it
246
- * is running on. This message will be displayed in the description area of the demo fragment
247
- * instead of the the provided {@link #getDescriptionResId()}. Additionally, all demos, both the
248
- * main demo and any additional demos will not be shown.
274
+ * <p> This message should provide insight into why the feature is restricted for the device it is
275
+ * running on. This message will be displayed in the description area of the demo fragment instead
276
+ * of the the provided {@link #getDescriptionResId()}. Additionally, all demos, both the main demo
277
+ * and any additional demos will not be shown.
249
278
*/
250
279
@ StringRes
251
280
public int getRestrictedMessageId () {
0 commit comments