Recycler view项进入动画无效。

6

我按照一个RecyclerView的动画教程进行操作,但是发现动画没有起效,现在有些困惑,不确定是否应用了动画。

Activity.Java: 更新的代码在这里,我尝试了使用按钮点击来调用动画方法,但我不知道如何从Adapter的onBindHolder方法中调用该动画方法。

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>

感谢您的提前支持。
3个回答

23

我晚了回答这个问题,但是我在按照同样的教程进行时遇到了同样的问题,经过尝试了许多不同的事情后,我意识到我没有在item_list_animation.xml文件中的set标签中添加属性:android:duration,这与您的情况相同。所以现在item_list_animation.xml XML变成了:

<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()都会取消动画。您可以通过简单地调用以下方式重新应用动画:

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

8
如果您正在使用Android架构组件中的LiveData和ViewModel,请确保在viewmodel.yourListLive().observe(){}中调用recyclerView.setLayoutAnimation(controller);。 例如这样:
  vm.getGamesPageList().observe(this, games -> {
    if (games == null)
      return;
    LayoutAnimationController animationController =
    AnimationUtils.loadLayoutAnimation(v.getContext(), ANIM_RES_ID);
    v.setLayoutAnimation(animationController);
    adapter.submitList(games);
    ...
  });

2
首先,您需要在RecyclerView中通过xml或编程方式加载动画。
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"                                        
android:layoutAnimation="@anim/layout_animation"/>

或者

LayoutAnimationController animation = 
AnimationUtils.loadLayoutAnimation(ctx, R.anim.layout_animation);
recyclerview.setLayoutAnimation(animation);

你还需要在OnBindViewHolder中调用该方法,例如:runLayoutAnimation(YOUR_RECYCLER_VIEW)

已经通过编程方式加载了动画,您可以在runLayoutAnimation函数中看到,但是我该如何在onBindViewHolder中调用该方法呢? - Anns Rahim
在onBindViewholder中,每当您需要执行某些操作时,您需要调用该方法。例如,如果您想要删除一行的某个项目的单击,则必须在onclickListener中调用该方法。 - Sharath kumar
同时,在适配器中你不需要那个RecyclerView对象。你可以通过yourViewHolder.itemView.getContext()获取上下文。 - Sharath kumar
我无法从适配器中调用该方法,因此需要在onCreate方法中调用该方法。 - Anns Rahim
当事件发生时,它用于为适配器添加动画效果。如果你在oncreate中调用它,你不会注意到动画效果,因为它只会发生一次。相反,你应该在事件发生时调用它,比如点击按钮时。这时它会正确地显示动画效果。 - Sharath kumar
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接