Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate activity transitions when using lists/grids/recycler views?

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?

like image 660
Guilherme Avatar asked Jan 16 '15 11:01

Guilherme


1 Answers

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.

like image 166
George Mount Avatar answered Oct 16 '22 09:10

George Mount