回收者视图项输入动画不起作用 [英] Recycler view item enter animation not working

查看:62
本文介绍了回收者视图项输入动画不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了有关为recyclerview项设置动画的教程,但是该动画无法正常工作,因为无论是否应用该动画,我都感到困惑.

I followed a tutorial for animating recyclerview item but the animation is not working iam confused that whether the animation is applied or not.

Activity.Java: 更新的代码在这里,我尝试单击按钮,但是我不知道如何从适配器的onBindHolder调用动画方法

Activity.Java: The updated code was here i tried with the button click but i dont know how to call the animation method from the onBindHolder form the adapter

private void setupRecyclerView() {
              RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        final LayoutAnimationController controller =
                AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation);

        recyclerView.setLayoutAnimation(controller);
        recyclerView.setAdapter(specialistListAdapter);
    }


  //placed a dummy button onclick to check animation working or not
 public void reloadData(View view) {
        runLayoutAnimation(recyclerView);
    }

private void runLayoutAnimation(final RecyclerView recyclerView) {
    final Context context = recyclerView.getContext();

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

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

layout_animation.xml:

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

item_list_animation.xml:

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

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

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

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

预先感谢

推荐答案

我回答这个问题很晚,但是在遵循相同的教程并且尝试了许多不同的事情之后,我遇到了同样的问题,我意识到我没有在您的 item_list_animation.xml 文件的set tag中添加了属性android:duration.因此, item_list_animation.xml XML变为现在:

I am late in answering this question but I was facing the same issue while following the same tutorial and after trying a bundle of different things I realized that I didn't added the attribute: android:duration in the set tag in item_list_animation.xml file which is same as your case. So the item_list_animation.xml XML becomes now:

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

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

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

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

</set>

注意:在更改数据集时,您需要在RecyclerView上重新应用动画,因为随时调用notifyDataSetChanged()会取消动画.您只需调用以下即可重新应用动画:

Note: on changing the dataset you need to reapply the animation on the RecyclerView because calling notifyDataSetChanged() at any time will cancel the animation. You can reapply the animation by simply calling:

adapter.notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();

这篇关于回收者视图项输入动画不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆