Transition of a common Android element between activity with a fragment to another activity with a fragment

I have an ActivityA containing FragmentA, and would like to make a transition of a common element in ActivityB containing FragmentB.

In actions, the toolbar will be a common element. In fragments, it will be text. Is there any way to do this?

If not, how can I make a general transition of an element between both fragments in actions?

Thanks in advance for any help. I am stuck.

Ok, so I will provide some code for clarification.

Here I have MainActivity:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <!-- This is shared with DetailActivity --> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" android:transitionName="sharedToolbar"/> </android.support.design.widget.AppBarLayout> <!-- This contains MainFragment --> <FrameLayout android:id="@+id/activity_main_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.design.widget.CoordinatorLayout> 

This operation contains this fragment called MainFragment:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- This element is shared with DetailFragment --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Fragment main" android:transitionName="sharedFragment"/> <!-- When this is clicked show next screen --> <Button android:id="@+id/fragment_main_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to detail screen" android:layout_gravity="bottom|center"/> </FrameLayout> 

Now what I want to do is when I click the button in MainFragment. I want to execute a fragment transaction for this action called DetailActivity:

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <!-- This is shared with MainActivity --> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" android:transitionName="sharedToolbar"/> </android.support.design.widget.AppBarLayout> <!-- This contains DetailFragment --> <FrameLayout android:id="@+id/activity_detail_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.design.widget.CoordinatorLayout> 

This operation contains this fragment called DetailFragment:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- This element is shared with MainFragment --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Fragment detail" android:transitionName="sharedFragment"/> </FrameLayout> 

As you can see in the code, both actions share a toolbar and have the same transition name. In their containing fragments there is a text that I would like to animate also at the transition. These text images also have the same transition name. Is there any way to do this? I tried, and so far I could only animate the toolbar between these two actions. How to make fragments perform a joint transition of an element at the same time?

If this is not possible, how can I do this when I move from MainActivity to DetailActivity, that the textual representation of the fragments is what appears as a transition, not a toolbar (if I cannot activate the activity of fragmentation and at the same time).

+7
android android-layout android-fragments android-animation
source share
2 answers

Take a look at the following lessons: https://github.com/lgvalle/Material-Animations

+2
source share

Facing a similar problem. My mistake is related to the architectural decision I made. One of the actions is the multiplicity of fragments for each individual stream. Now I can’t have the desired transitions when switching between threads, because it required loading activity and fragment.

First, change the background color of your activity fragment to the default background color of your application. The default background color of my application was gray, and the white background of container activity looked very bad. The gray background looks less bad.

Now remove the default transition from the container activity. This can be done using overridePendingTransition(0,0);

Now everything will look ugly. It is just a job.

0
source share

All Articles