如何从漂亮的动画中删除RecyclerView中的所有项目

时间:2018-03-30 10:37:27

标签: android android-recyclerview android-animation

我必须通过在列表视图中展开和折叠动画来实现列表视图。我有一个方法使用这样做,但它不适用于RecyclerView视图。整个RecyclerView将在短时间内可见,动画在此之后开始。 Refer the GIF in this link

作为替代方案,我会遵循此post并添加和删除所有项目(它总是会少于5个项目,因此没有性能问题)使用
LayoutAnimation取得了以下成就。

enter image description here

如您所见,列表视图项显示动画正常工作。隐藏项目反转动画文件中的属性,如下所示,并在删除RecyclerView

中的所有数据后应用它

item_remove_animation_drop_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@integer/anim_duration_medium">

        <translate
        android:fromYDelta="0"
        android:toYDelta="-20%"
        android:interpolator="@android:anim/decelerate_interpolator" />

        <alpha
            android:fromAlpha="1"
            android:toAlpha="0"
            android:interpolator="@android:anim/decelerate_interpolator"
            />

        <scale
            android:fromXScale="100%"
            android:fromYScale="100%"
            android:toXScale="95%"
            android:toYScale="95%"
            android:pivotX="50%"
            android:pivotY="50%"
            android:interpolator="@android:anim/decelerate_interpolator" />

    </set>

layout_animation_fall_down.xml

<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_add_animation_fall_down"
    android:delay="15%"
    android:animationOrder="normal" />

适配器中的方法

public void runItemRemoveAnimation(final RecyclerView recyclerView) {

        final LayoutAnimationController controller =
                AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_drop_down);

        recyclerView.setLayoutAnimation(controller);
        recyclerView.getAdapter().notifyDataSetChanged();
        recyclerView.scheduleLayoutAnimation();
    }

片段中的代码

private void onClickSelectPromotionContainer() {
        selectPromotionContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (discountListExpanded) {

                    promoListData.removeAll(promosDataFromServer);
                    discountListAdapter.runItemRemoveAnimation(discountRecyclerView);
                    selectPromotionImage.startAnimation(rotatePlus180);
                    discountListExpanded = false;

                } else {

                    discountRecyclerView.setVisibility(View.VISIBLE);
                    promoListData.addAll(promosDataFromServer);
                    discountListAdapter.runItemAddAnimation(discountRecyclerView);
                    selectPromotionImage.startAnimation(rotateMinus180);
                    discountListExpanded = true;

                }
            }
        });
    }

但它没有创建任何动画。我认为因为它删除了我认为的所有数据。那么有什么方法可以在清除列表视图时添加漂亮的动画,可以用来说服用户列表隐藏?或者我在代码中做错了什么?它应该与添加动画的项目相反,以保持它的美观,并且应该是垂直动画。

0 个答案:

没有答案
相关问题