Reputation: 1084
I'm trying to switch 2 fragments using some animation. Everything works if I have a layout with just one fragment, but when I add two fragments one after another then this transition does not work.
My activity layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="myapplication.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/theMap"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="@+id/MainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Now I want to change fragment in the MainFrame container, however the result is that the fade out off the fragment is wrong. The content of the first fragment is longer than the second. When the transition is done, it cut the first fragment to size of the second one and then the fade effect is done. What I want to achieve is that the first fragment is fade out in its full height and then the second fragment is added in its height using animation.
Please note that this works if I dont have the LinearLayout and have only MainFrame, then the fade out/fade in is correct, but the LinearLayout (or constraint layout) just ruins it.
This is the transition code:
private void performTransition()
{
if (isDestroyed())
{
return;
}
Fragment previousFragment = getSupportFragmentManager().findFragmentById(R.id.MainFrame);
Fragment nextFragment = BlankFragment2.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
long FADE_DEFAULT_TIME = 2000;
Fade exitFade = new Fade();
exitFade.setDuration(FADE_DEFAULT_TIME);
previousFragment.setExitTransition(exitFade);
Fade enterFade = new Fade();
enterFade.setStartDelay(FADE_DEFAULT_TIME);
enterFade.setDuration(FADE_DEFAULT_TIME);
nextFragment.setEnterTransition(enterFade);
fragmentTransaction.replace( R.id.MainFrame, nextFragment);
fragmentTransaction.commitAllowingStateLoss();
}
To recreate this issue, just create a new android studio project, select Scrolling activity, then add 3 blank fragments, set the long content to one fragment's textView and observe the behavior.
What to do to have proper fade out/fade in animation in case of a fragments in linearLayout?
Upvotes: 0
Views: 241
Reputation: 1084
The solution for this is to set android:fillViewport="true" to ScrollView, because the it seems that the scrollview first resize itself and cut animation on the rest of the screen.
Upvotes: 0