0

I am trying to make Share Element Transition for RecyclerView element , using the image as the share element.

I am able to make other transition work except the Share Element Transition.

my guess is that the problem is on in Transition.hide and transition.Show.

other that that i am stuck. please help :)

Here is the method that handles the transition

public void goToProduct(ProductItem current) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Transition changeTransform = TransitionInflater.from(this).
                    inflateTransition(R.transition.change_image_transform);
            Transition explodeTransform = TransitionInflater.from(this).
                    inflateTransition(android.R.transition.explode);

            productListFragment.setSharedElementReturnTransition(changeTransform);
            productListFragment.setExitTransition(explodeTransform);

            productPageFragment.setSharedElementEnterTransition(changeTransform);
            productPageFragment.setEnterTransition(explodeTransform);

            isProductPageOpenedFromCart = false;
            isProductPageOpenedFromList = false;

            ImageView  listImage = (ImageView) findViewById(R.id.listImg1);
            ImageView  pageImage = (ImageView) findViewById(R.id.pageImg);
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.hide(productListFragment)
                    .addToBackStack("transaction")
                    .addSharedElement(listImage, "MyTransition");

            if (isCartOpen) {
                CartFragment cartFragment = (CartFragment) manager.findFragmentByTag(CART_FRAGMENT_TAG);
                transaction.remove(cartFragment);
                isCartOpen = false;
                isProductPageOpenedFromCart = true;
            } else {
                isProductPageOpenedFromList = true;
            }
            productPageFragment.setProduct(current);
            transaction.show(productPageFragment).addToBackStack("transaction")
                    .addSharedElement(pageImage, "MyTransition");;
            transaction.commit();
        }

        else {

the transition folder file is named change_image_transform and contains:

<?xml version="1.0" encoding="utf-8"?>
    <transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
        <changeTransform/>
        <changeImageTransform/>
    </transitionSet>

I also did use android:transitionName="MyTransition" on boath images.

And here are lines of code added to style.xml

<item name="android:windowContentTransitions">true</item>
            <item name="android:windowEnterTransition">@transition/change_image_transform</item>
            <item name="android:windowExitTransition">@transition/change_image_transform</item>
            <item name="android:windowSharedElementEnterTransition">@transition/change_image_transform</item>
            <item name="android:windowSharedElementExitTransition">@transition/change_image_transform</item>
Dragos Cocos
  • 49
  • 2
  • 7

1 Answers1

0

In RecyclerView you propably have many views with id R.id.listImg1 You have to in adapter set sharedView setTransitionName programatycally for example you can use your product id for creating transition name. And when you creating second details fragment you can pass by bundle transition name to that gragment. In onCrete you can set image transitionname to the same like before.

Or you can create:

 public interface IRecyclerClikListener {

   public void onItemClik(int pos,Object obj,View view);
 }

Add this listener to your recycler. To the view you pass a refrence to clikced row. If you have reference to cliked view you can set transition name to image belongs to this view. Dont forget to set the same transition name in second fragment.

Adam Miśtal
  • 715
  • 4
  • 15
  • My reputation is only 14 so i can't vote for it yet, but it worked just fine – Dragos Cocos Sep 25 '15 at 06:06
  • @DragosCocos solution worked ? Kindly help here https://stackoverflow.com/questions/47742560/show-a-fragment-with-shared-elements-animation – arul Feb 09 '18 at 08:03