I am trying to implement some of the new Material Design activity animations, but the tutorials I've read only show examples where the view to be animated belong to the activity.
In my app, I am using a RecyclerView
, so the ImageView
is not part of the activity:
MainActivity -> Where I call startActivity()
↳ MainFragment
↳ RecyclerView
↳ RecyclerViewAdapter
↳ ViewHolder
↳ ImageView -> The hero imageView I'd like to animate
From what I read, I am supposed to start the new activity like this:
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, (View) mImageView, "hero_image");
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, options.toBundle());
The question is: that mImageView
does not belong to the MainActivity
. How am I supposed to pass it?
In your example, you start the Activity in the same way. The View is part of the View Hierarchy of the Activity, so the call is still valid. When there is an exit transition, the Activity's View Hierarchy is searched for exiting Views to be used in the exit transition. It compares objects with those that are shared elements so that it doesn't exit the shared elements.
When you're using a RecyclerView, you will likely have to worry about the reenter transition. The RecyclerView can recycle any or all of the Views. If you haven't given your shared elements (or potential shared elements) unique names, you'll have to implement onMapSharedElements to ensure that the correct View is used. I always recommend that when using lists of potential shared elements that you give every element a unique transitionName (dynamically). That way the framework can automatically determine which View to use when coming back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With