Search code examples
androidandroid-5.0-lollipopandroid-transitions

new Shared Transition API not working for Imageview from One recycler view to another


I am trying to achieve the sharedElement to animate an imageView from one activity to another , but my problem is my imageView is inside recyclerView header so it doesn't animate.

So my question is, Is there a way for this to achieve?

I have seen many apps on PlayStore doing it

Code: While starting 2nd activity

 String transitionName = getString(R.string.demo);

ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, v, transitionName);
startActivity(intent, transitionActivityOptions.toBundle());

and i have set:

ViewCompat.setTransitionName(icon, convertView.getContext().getString(R.string.demo));

to both my first activity grid and 2nd activity recyclerView Header


Solution

  • If your First acitivty imageview transition names have to be unique for the transition animation to work. In the Recycler grid onBindView method you have to assign transition name for each imageview seperately using below code

    holder.gridImageView.setTransitionName("gridImageTransition" + position);
    

    Am adding the position to each grid image transition name to make it unique.

    In ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, v, transitionName);

    this transition name should be the same as the transition name you assign to Recycler view header in second activity using

        You will have to set the sharedTransition name for the imageView in your header after it has been inflated. Like below. Setting in xml wont work for the reason that it wont be immediately inflated.
    

    Inside onBindViewHolder of your recycler adapter use

     holder.imageView.setTransitionName(transition);