执行视图切换时无法在ViewAnimator中实现动画效果

Unable to achieve animation effect in ViewAnimator when performing view switching

ViewAnimator中进行视图切换时,我首先想到的是我可以很容易地实现动画效果,通过使用

viewAnimator.setDisplayedChild(1);

viewAnimator.showNext();

视图切换成功。但是,没有显示动画。

我预计会有滑入/滑出效果,因为我在初始化期间设置了动画。

Animation slideInLeftFast = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_in_left_fast);
Animation slideOutRightSlow = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_out_right_slow);
this.viewAnimator.setInAnimation(slideInLeftFast);
this.viewAnimator.setOutAnimation(slideOutRightSlow);
slideInLeftFast.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationEnd(Animation arg0) {
        android.util.Log.i("CHEOK", "animation end");
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
        android.util.Log.i("CHEOK", "animation repeat");
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
        android.util.Log.i("CHEOK", "animation start");
    }
});

我的完整布局文件如下。 ViewAnimator

中有 2 个不同的视图

trading_sign_in_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view_animator"
    android:animateFirstView="false"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/sign_in_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="0dp"
            android:layout_above="@+id/sign_in_bottom_nav_bar">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/username_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Username"
                    android:id="@+id/username_edit_text"

                    android:inputType="textVisiblePassword|textNoSuggestions"
                    android:imeOptions="actionNext|flagNoExtractUi" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                app:passwordToggleEnabled="true"
                android:id="@+id/password_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:id="@+id/password_edit_text"

                    android:inputType="textPassword"
                    android:imeOptions="actionNext|flagNoExtractUi" />
            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:id="@+id/forgot_password_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:enabled="true"
            android:textAllCaps="false"
            android:text="Forgot password"
            android:textSize="16sp"
            android:layout_above="@+id/sign_in_bottom_nav_bar"
            android:layout_centerHorizontal="true"

            android:layout_marginBottom="8dp"
            android:paddingLeft="32dp"
            android:paddingRight="32dp" />

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/sign_in_bottom_nav_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                style="?android:attr/buttonBarButtonStyle"
                android:background="?attr/selectableItemBackground"

                android:id="@+id/sign_in_button"
                android:layout_width="0dp"
                android:width="0dp"
                android:layout_weight="1.0"
                android:layout_height="48dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:enabled="true"
                android:textAllCaps="true"
                android:text="Log in" />
        </LinearLayout>
    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/forgot_password_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="0dp"
            android:layout_above="@+id/forgot_password_bottom_nav_bar">

            <TextView
                android:id="@+id/primary_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="16sp" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/forgot_password_username_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Username"
                    android:id="@+id/forgot_password_username_edit_text"

                    android:inputType="textVisiblePassword|textNoSuggestions"
                    android:imeOptions="actionNext|flagNoExtractUi" />
            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/forgot_password_bottom_nav_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                style="?android:attr/buttonBarButtonStyle"
                android:background="?attr/selectableItemBackground"

                android:id="@+id/forgot_password_forgot_password_button"
                android:layout_width="0dp"
                android:width="0dp"
                android:layout_weight="1.0"
                android:layout_height="48dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:enabled="true"
                android:textAllCaps="true"
                android:text="Forgot password" />
        </LinearLayout>
    </RelativeLayout>

</ViewAnimator>

请问我的代码有什么问题。我错过了什么吗?完整的最小代码可以在 https://github.com/yccheok/MyProject

找到

如果您在 viewAnimator 初始化后仅指定初始显示的子项,动画将按预期启动。

viewAnimator = (ViewAnimator) v.findViewById(R.id.view_animator);
viewAnimator.setDisplayedChild(0);