简体   繁体   English

在linearlayout中的两个片段中淡出效果不佳

[英]fade out in two fragments in linearlayout not working as expected

I'm trying to switch 2 fragments using some animation. 我正在尝试使用一些动画切换2个片段。 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. 现在,我想更改MainFrame容器中的片段,但是结果是片段淡出是错误的。 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. 请注意,如果我没有LinearLayout而只有MainFrame,则此方法有效,则淡入/淡入是正确的,但是LinearLayout(或约束布局)只会破坏它。

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. 要重现此问题,只需创建一个新的android studio项目,选择“滚动活动”,然后添加3个空白片段,将长内容设置为一个片段的textView并观察行为。

What to do to have proper fade out/fade in animation in case of a fragments in linearLayout? 在linearLayout中有片段的情况下,如何使动画具有适当的淡入/淡出效果?

解决方案是将android:fillViewport =“ true”设置为ScrollView ,因为似乎scrollview首先调整其大小并在屏幕的其余部分剪切动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM