首页 > 解决方案 > recyclerview 问题上的 Alpha 动画 - 淡入为零后弹回

问题描述

还有一个问题。我试图让 RecycleView 项目消失,所以我为每个项目编写了一个 XML 动画,然后是一个层动画 XML,并在 Java 主要活动中添加了代码。一切正常,视图消失了,但问题是,在它们全部消失后,它们突然又自行出现了!可能是什么原因?如何保持 alpha 0?

代码:

主要活动方式:

private void layoutDisappear(final RecyclerView recyclerView) {
        if(recyclerView.isAnimating()){
            return;
        }
        final Context context = recyclerView.getContext();
        final LayoutAnimationController controller =
                AnimationUtils.loadLayoutAnimation(context, R.anim.layout_recycleview_disappear);


        recyclerView.setLayoutAnimation(controller);
        recyclerView.setLayoutAnimationListener(new Animation.AnimationListener() {
                                       public void onAnimationStart(Animation animation) {}
                                       public void onAnimationRepeat(Animation animation) {}
                                       public void onAnimationEnd(Animation animation) {
                                       }
                                   });
                recyclerView.scheduleLayoutAnimation();

项目动画 XML:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1400">
    <alpha
        android:fromAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="0" />
    <scale
        android:fromXScale="100%"
        android:fromYScale="100%"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="102%"
        android:toYScale="102%" />
</set>

层 XML:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_recycleview_disappear"
    android:animationOrder="reverse"
    android:delay="7%" />

标签: androidandroid-animationandroid-transitions

解决方案


您需要android:fillAfter="true"向 Item 动画 XML 添加属性以保持动画更改。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:duration="1400">
    ...
</set>

推荐阅读