Recycler视图项目输入动画不起作用

时间:2017-10-11 06:50:30

标签: android android-layout android-recyclerview

我按照动画reclerview项目的教程,但动画无法正常工作,我不知道动画是否应用。

Activity.Java: 更新的代码在这里我尝试按钮单击,但我不知道如何从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 个答案:

答案 0 :(得分:2)

首先,您需要在回收站视图中以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)

答案 1 :(得分:2)

我回答这个问题很晚,但是在遵循相同的教程时,我遇到了同样的问题,并且尝试了许多不同的事情之后,我意识到我没有在{{ 1}}在 item_list_animation.xml 文件中,与您的情况相同。因此, item_list_animation.xml XML现在变为:

android:duration

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

<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>

答案 2 :(得分:0)

如果您使用的是LiveDataViewModel(来自Android Architecture Components),请确保您在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);
    ...
  });