Booyaches
Booyaches

Reputation: 1719

How to make content transition with fragments?

Upon transaction from fragment A to B, I'd like some specific view of fragment B to move from point X to point Y. That should be possible to achieve using content transition: Frament.setEnterTransition(Object transition). I don't understand what should I pass to setEnterTransition(Object transition) in order to tell the specific view of B to animate in a specific way.

A lot can be found on the web but it mostly refers to shared element transition, I can't find any help with content transition.

Upvotes: 3

Views: 737

Answers (2)

azizbekian
azizbekian

Reputation: 62209

Assuming you have this as your fragment's layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:background="@android:color/white"
             android:layout_height="match_parent">

    <View
            android:id="@+id/shared_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical|right"
            android:background="#812432"/>

    <View
            android:id="@+id/non_shared_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="80dp"
            android:layout_gravity="center_vertical|right"
            android:background="#fff53f"/>

</FrameLayout>

And you perform this from your first fragment, that initiates opening of second fragment:

Slide slide = new Slide(Gravity.LEFT);
TransitionSet set = new TransitionSet();
set.addTransition(slide);
set.addTarget(R.id.shared_view);
Fragment secondFragment = new SecondFragment();
secondFragment.setEnterTransition(set);
getActivity().getSupportFragmentManager().beginTransaction()
             .add(R.id.contentPanel, secondFragment)
             .addToBackStack(null)
             .commit();

You'll see, that shared_view will slide from left to it's initial position, while non_shared_view will stay in it's place.

enter image description here

Source code.

Upvotes: 2

Aravindraj
Aravindraj

Reputation: 590

Content Transitions in explained in following website http://www.androiddesignpatterns.com/2014/12/activity-fragment-content-transitions-in-depth-part2.html

Upvotes: 0

Related Questions