动画片段导致其他视图“跳跃”

时间:2016-07-06 09:55:16

标签: android android-fragments animation transition

简短问题:

在FragmentTransactions上使用动画时,如何使用动画为其他视图设置动画?

长问题:

您好,

我是片段之类的新手,等等,我试图在单个活动中为它们设置动画,因此我为活动创建了以下xml文件:

<LinearLayout
    android:id="@+id/run_select_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:orientation="vertical">


    <FrameLayout
        android:id="@+id/activity_run_search_fragmentHeaderPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/activity_run_search_fragmentPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

然后我定义了我想用于动画的res / anim文件:

fade_in_from_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_longAnimTime" />

    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="-100%" android:toYDelta="0%"
        android:duration="1000" />
</set>

fade_in_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_longAnimTime" />

    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="1000"/>
</set>

fade_out_to_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <alpha
        android:duration="@android:integer/config_longAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="100%" />
</set>

fade_out_to_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <alpha
        android:duration="@android:integer/config_longAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="-100%" />
</set>

详细说明: 下部片段始终包含带有多个项目的列表视图。我会根据切换按钮搜索的内容更改片段。那没问题。当用户点击特定的监听时,headerFragment将填充其他数据,并显示淡入淡出的动画,如:

首先在加载数据时显示加载片段:

final RSBaseFragment headerFragment = (RSBaseFragment) getSupportFragmentManager().findFragmentById(R.id.activity_run_search_fragmentHeaderPlaceholder);

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
LoadingFragment fragment = new LoadingFragment();

if (headerFragment != null) {
    ft.setCustomAnimations(R.anim.fade_in_from_bottom, R.anim.fade_out_to_top, R.anim.fade_in_from_top, R.anim.fade_out_to_bottom);
    ft.replace(R.id.activity_run_search_fragmentHeaderPlaceholder, fragment, HEADER_FRAGMENT_TAG);
} else {
    ft.setCustomAnimations(R.anim.fade_in_from_top, R.anim.fade_out_to_bottom, R.anim.fade_in_from_bottom, R.anim.fade_out_to_top);
    ft.add(R.id.activity_run_search_fragmentHeaderPlaceholder, fragment, HEADER_FRAGMENT_TAG);
}
ft.commit();

加载数据后,显示替换加载片段的新片段中的数据

HeaderFragment fragment = new HeaderFragment();
ft.setCustomAnimations(R.anim.fade_in_from_top, R.anim.fade_out_to_bottom, R.anim.fade_in_from_bottom, R.anim.fade_out_to_top);
ft.replace(R.id.activity_run_search_fragmentHeaderPlaceholder, fragment, HEADER_FRAGMENT_TAG);

问题在于,headerfragment正在从顶部或底部方向褪色并滑入,而持有listview的片段在它甚至存在之前“跳”到headerfragment的底部Y.如何使用标题片段为下部片段设置幻灯片以创建流畅的用户体验?

很抱歉这个问题很长。如果搜索了3天,并没有找到任何帮助解决我的问题。

1 个答案:

答案 0 :(得分:0)

我几乎通过添加到LinearLayout(run_select_fragment_container)来解决它

android:animateLayoutChanges="true"

在活动的onCreate中,我已经完成了这个

LinearLayout mainLayout = (LinearLayout) findViewById(R.id.run_select_fragment_container);
LayoutTransition layoutTransition = mainLayout.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);