0

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

JAAD
  • 12,349
  • 7
  • 36
  • 57

2 Answers2

1

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);
Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
0

If your MainActivity has an RecyclerView adapter like this:

public class MainActivity extends Activity {
    private SampleRecyclerViewAdapter adapter;    

    public void onCreate(Bundle savedBundle) {
        RecyclerView recycleView = (RecyclerView) findViewById(R.id.recycle_view);
        SampleRecyclerViewAdapter(this);
        recycleView.setAdapter(adapter);

    }
} 

In Adapter Class,

public class public class SampleRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    public SampleRecyclerViewAdapter(Context ctx) {
        this.mContext = ctx;
    }
    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Initiate ViewHolder and return it.
        return null;
    }    
    @Override
    public void onBindViewHolder(VH viewHolder, int i) {
        viewHolder.view.setOnClickListener(
        new OnClickListener() {
            @Override
            public void onClick() {
                ActivityOptionsCompat transitionActivityOptions =
                ActivityOptionsCompat.makeSceneTransitionAnimation(this.mContext, viewHolder.view, transitionName);
                startActivity(intent, transitionActivityOptions.toBundle());
                ViewCompat.setTransitionName(icon, convertView.getContext().getString(R.string.demo));
            }
        }
        );
    }
}

In this code, SharedElementTransition code is implemented in adapter class.

Abdul Rahman K
  • 664
  • 5
  • 16