3

I have the requirement of 2 activities in which 1 activity is having fragments. I want the zoom like animation (Shared element Transition) to be start from the fragment in Activity1 and end at Activity2.

Here is the code:

Activity 1: SourceGridFragmentActivity.java

 @Override
  public void gridItemOnFragmentClicked(ImageView sharedImage) {
    Intent intent = new Intent(this, DestinationActivity.class);
    intent.putExtra(Constants.KEY_INTENT_TRANSITION_NAME,
        ViewCompat.getTransitionName(sharedImage));

    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
        this,
        sharedImage,
        ViewCompat.getTransitionName(sharedImage));

    startActivity(intent, options.toBundle());
  }

Fragment inside Activity 1: FragmentClassForGrid.java

private void initializeGridView() {
    gridBinding.gridView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    gridBinding.gridView.setAdapter(new GridAdapter(getActivity(), this));
  }

  @Override
  public void onGridItemClick(ImageView sharedImageView) {
    activityView.gridItemOnFragmentClicked(sharedImageView);
  }

Recycler Adapter: GridAdapter.java

@Override
  public void onBindViewHolder(
          ViewHolder holder, int position) {
    ViewCompat.setTransitionName(holder.imageView, Constants.TRANSITION_NAME_IMAGE_TO_ACTIVITY);
    holder.imageView.setOnClickListener(v ->
        gridAcitvityView.onGridItemClick(holder.imageView));
  }

Activity 2 where end of animation is desired: DestinationActivity.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    destBinding = DataBindingUtil.setContentView(this, R.layout.activity_destination);
    supportPostponeEnterTransition();
    initializeZoomView();
  }

  private void initializeZoomView() {
    Bundle extras = getIntent().getExtras();
    destBinding.destinationImageView
        .setTransitionName(extras.getString(Constants.KEY_INTENT_TRANSITION_NAME));
    supportStartPostponedEnterTransition();
    destBinding.destinationImageView
        .setImageDrawable(getResources().getDrawable(R.drawable.model));
  }
khushboo
  • 45
  • 1
  • 6
  • You did not use `android:transitionName` anywhere in xml and Also Do not add links to code . Just add the relevant code here. And to answer your question i suggest you should follow some tutorials [Here is one](http://mikescamell.com/shared-element-transitions-part-2/) . – ADM Jun 22 '18 at 12:28
  • @ADM Edited with the relevent code here. Also, If I am doing this without taking fragment, then it is working fine. But creating issues while moving back from Activity2 to Fragment in Activity1. The tutorial provided by you is having fragment to fragment or activity to activity transition. Not Fragment to Activity. – khushboo Jun 22 '18 at 12:54

0 Answers0