@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Item item = (Item) adapterView.getItemAtPosition(position);
// Construct an Intent as normal Intent intent = new Intent(this, DetailActivity.class); intent.putExtra(DetailActivity.EXTRA_PARAM_ID, item.getId());
// BEGIN_INCLUDE(start_activity) /** * Now create an {@link android.app.ActivityOptions} instance using the * {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory * method. */ ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation( this,
// Now we provide a list of Pair items which contain the view we can transitioning // from, and the name of the view it is transitioning to, in the launched activity new Pair<View, String>(view.findViewById(R.id.imageview_item), DetailActivity.VIEW_NAME_HEADER_IMAGE), new Pair<View, String>(view.findViewById(R.id.textview_name), DetailActivity.VIEW_NAME_HEADER_TITLE));
// Now we can start the Activity, providing the activity options as a bundle ActivityCompat.startActivity(this, intent, activityOptions.toBundle()); // END_INCLUDE(start_activity) }
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/** * Our secondary Activity which is launched from {@link MainActivity}. Has a simple detail UI * which has a large banner image, title and body text. */ public class DetailActivity extends Activity {
// Extra name for the ID parameter public static final String EXTRA_PARAM_ID = "detail:_id";
// View name of the header image. Used for activity scene transitions public static final String VIEW_NAME_HEADER_IMAGE = "detail:header:image";
// View name of the header title. Used for activity scene transitions public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title";
// BEGIN_INCLUDE(detail_set_view_name) /** * Set the name of the view's which will be transition to, using the static values above. * This could be done in the layout XML, but exposing it via static variables allows easy * querying from other Activities */ ViewCompat.setTransitionName(mHeaderImageView, VIEW_NAME_HEADER_IMAGE); ViewCompat.setTransitionName(mHeaderTitle, VIEW_NAME_HEADER_TITLE); // END_INCLUDE(detail_set_view_name)
loadItem(); }
private void loadItem() { // Set the title TextView to the item's name and author mHeaderTitle.setText(getString(R.string.image_header, mItem.getName(), mItem.getAuthor()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && addTransitionListener()) { // If we're running on Lollipop and we have added a listener to the shared element // transition, load the thumbnail. The listener will load the full-size image when // the transition is complete. loadThumbnail(); } else { // If all other cases we should just load the full-size image now loadFullSizeImage(); } }
/** * Try and add a {@link Transition.TransitionListener} to the entering shared element * {@link Transition}. We do this so that we can load the full-size image after the transition * has completed. * * @return true if we were successful in adding a listener to the enter transition */ private boolean addTransitionListener() { final Transition transition = getWindow().getSharedElementEnterTransition();
if (transition != null) { // There is an entering shared element transition so add a listener to it transition.addListener(new Transition.TransitionListener() { @Override public void onTransitionEnd(Transition transition) { // As the transition has ended, we can now load the full-size image loadFullSizeImage();
// Make sure we remove ourselves as a listener transition.removeListener(this); }
@Override public void onTransitionStart(Transition transition) { // No-op }
@Override public void onTransitionCancel(Transition transition) { // Make sure we remove ourselves as a listener transition.removeListener(this); }
@Override public void onTransitionPause(Transition transition) { // No-op }