Shared element transition between fragment in ViewPager and new fragment

Viewed 1484

Is there any way to use a shared element transitions from a fragment inside a ViewPager into a new Fragment?

EDIT

I have a MainActivity with the ViewPager that holds FragmentA. From that I want to open FragmentB with a Shared Element Transition.

I've created a sample project which you can see below. The shared element transition is working fine if I remove the ViewPager and open FragmentA regularly, but as soon as I add the ViewPager to the project, no transition appears.

This is the code for MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager mViewPager = (ViewPager)findViewById(R.id.viewpager);

    FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        private int NUM_ITEMS = 1;
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new FragmentA();
                default:
                    return null;
             }
        }
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
    };
    mViewPager.setAdapter(adapter);
    }
}

XML for MainActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gustafwennerstrom.testeb.MainActivity">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:id="@+id/viewpager"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

This is the code for FragmentA

public class FragmentA extends Fragment {
    FrameLayout root;

    public FragmentA() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        root = (FrameLayout)inflater.inflate(R.layout.fragment_, null);
        ImageView image = (ImageView) root.findViewById(R.id.testImage);
        image.setOnClickListener(openFragment);
        return root;

    }

    private View.OnClickListener openFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView sharedImage = (ImageView)root.findViewById(R.id.testImage);
            getFragmentManager()
                .beginTransaction()
                .addSharedElement(sharedImage, ViewCompat.getTransitionName(sharedImage))
                .addToBackStack(getTag())
                .replace(android.R.id.content, new FragmentB())
                .commit();
    };
}

XML for FragmentA (FragmentB looks very similar)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_a"
    android:background="@color/colorAccent"
    tools:context="com.example.gustafwennerstrom.testeb.FragmentA">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/yo"
        android:transitionName="myImage"
        android:id="@+id/testImage"/>
</FrameLayout>

This is fragmentB

public class FragmentB extends Fragment {
    public FragmentB() {}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setSharedElementEnterTransition(TransitionInflater
            .from(getContext())
            .inflateTransition(android.R.transition.move)
            .setDuration(100));
         }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_fragment_b, container, false);

    }
}

And in styles.xml I've added this line of code:

<item name="android:windowContentTransitions">true</item>
1 Answers
Related