How to animate activity transitions when viewing lists / grids / recyclers?

I am trying to implement some of the new Material Design activity animations, but the tutorials that I read show only examples in which the animated presentation refers to the activity.

In my application, I use RecyclerView , so ImageView 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 have to start a new activity as follows:

 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 MainActivity . How should I pass it on?

+6
source share
1 answer

In your example, you start Activity in the same way. The view is part of the activity presentation hierarchy, so the call is still valid. When a transition occurs at the exit, a hierarchy of activities is searched to exit the representations that will be used in the transition process. It compares objects with those that are common elements, so that it does not go out of common elements.

When you use RecyclerView, you may have to worry about re-transitioning. RecyclerView can recycle any or all kinds. If you did not specify unique names for common elements (or potential common elements), you need to implement onMapSharedElements to make sure that the correct view is used. I always recommend that when using lists of potential common elements that you give each element a unique transition name (dynamically). In this way, the infrastructure can automatically determine which species to use when returning.

+1
source

Source: https://habr.com/ru/post/981106/


All Articles