1

我在我的布局中使用了一个片段,我想要滑入,滑出片段的动画。

这就是我的片段的样子

 <fragment
            android:id="@+id/fragment_place"


            android:name="com.mainpackage.FragmentOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

这是我在动画 xml 文件中的幻灯片

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="-1000dp" android:valueTo="0dp"
        android:valueType="floatType"
        android:propertyName="translationX"
</set>

这是我的滑出动画 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0dp" android:valueTo="1000dp"
        android:valueType="floatType"
        android:propertyName="translationX"
        android:duration="@android:integer/config_shortAnimTime" />


</set>

然而,这种转变似乎很奇怪。似乎第一个片段(FragmentOne)总是在那里。当调用过渡时,FragmentOne 会显示一段时间,然后是动画中的幻灯片。 没有可见的滑出动画。

我已经读过不建议像在我的代码中那样静态修复片段,但这就是我得到不正确动画的原因吗?我应该怎么做才能修复过渡?

这是我的切换代码:

FragmentManager fm = getFragmentManager();
         FragmentTransaction fragmentTransaction = fm.beginTransaction();



         if(oldt!=t)
         {  fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
         oldt=t;
         }
         else
         {
             fragmentTransaction.setCustomAnimations(R.anim.none, R.anim.none);
         }
         fragmentTransaction.replace(R.id.fragment_place, fr);
         fragmentTransaction.commit();
4

1 回答 1

0

您应该使用框架布局作为容器并以编程方式设置片段。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    tools:context="com.wework.mobile.v2.activities.help.FailRequestFragment">

然后在代码中

final FragmentTransaction transaction = fragmentManager.beginTransaction();

        // Replace whatever is in the container view with this
        // fragment, and add the transaction to the back stack so the user
        // can navigate back
        transaction.replace(R.id.container, fragment);
        if (add_to_backstack) transaction.addToBackStack(null);

     if(oldt!=t)
     {  fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
     oldt=t;
     }
     else
     {
         fragmentTransaction.setCustomAnimations(R.anim.none, R.anim.none);
     }

        // Commit the transaction
        transaction.commit();

所以是的,您应该使用 FrameLayout 而不是布局中的固定片段,并以编程方式将片段放入布局中。

于 2014-05-27T21:03:09.497 回答