【问题标题】:recyclerview animation not working with objectanimatorrecyclerview 动画不适用于 objectanimator
【发布时间】:2016-11-04 19:13:39
【问题描述】:

enter code here我想使用ObjectAnimatorRecyclerview 中的项目实现动画。我关注了this tutorial

这是我获取动画的代码:

public static void animate(RecyclerView.ViewHolder holder, boolean direction)
{
    AnimatorSet animatorSet=new AnimatorSet();
    ObjectAnimator animator_Y=ObjectAnimator.ofFloat(holder.itemView,"TranslationY",direction==true ? 100 : -100,0);
    animator_Y.setDuration(1000);

    ObjectAnimator animator_X=ObjectAnimator.ofFloat(holder.itemView,"TranslationX",-20,20,0);
    animator_X.setDuration(1000);

    animatorSet.playTogether(animator_X,animator_Y);
    animatorSet.start();
    Log.e("animation","animation running");
}

我从onBindViewHolder() 调用这个函数。 translationX 和 translationY 没有动画。而且我确信每次滚动都会调用此方法。

我想知道ObjectAnimator 是否用于Cardview。谢谢。`

这是我的 onBindViewHolder 方法

   @Override
public void onBindViewHolder(ViewHolder holder, int position) {
  holder.textView.setText(data.get(position));
    AnimationforAdapters.animate(holder,true);
}

【问题讨论】:

  • 尝试将TranslationXTranslationY分别替换为translationXtranslationY
  • 我已更改为translationX和translationY,但没有出现动画。感谢harry的帮助。

标签: android android-recyclerview android-animation material-design objectanimator


【解决方案1】:

试试下面的代码:

public static void animate(RecyclerView.ViewHolder holder, boolean direction) {
    AnimatorSet animatorSet=new AnimatorSet();
    ObjectAnimator animator_Y=ObjectAnimator.ofFloat(holder.itemView,"translationY",direction ? 100 : -100,0);
    animator_Y.setDuration(1000);

    ObjectAnimator animator_X=ObjectAnimator.ofFloat(holder.itemView,"translationX",-20,20,0);
    animator_X.setDuration(1000);

    animatorSet.playTogether(animator_X,animator_Y);
    animatorSet.start();
    Log.e("animation","animation running");
}

ObjectAnimator 的android 文档中所述

ValueAnimator 的这个子类提供对目标对象的动画属性的支持。此类的构造函数采用参数来定义将被动画化的目标对象以及将被动画化的属性的名称。然后在内部确定适当的 set/get 函数,动画将根据需要调用这些函数来为属性设置动画。

所以,您的代码中的问题是ObjectAnimator 在您的目标对象中找不到可以动画的属性TranslationXTranslationY

【讨论】:

  • 我试过translationX和translationY,卡片列表视图没有变化。 recyclerview的卡数有限制吗??
  • 不,没有这样的限制
【解决方案2】:

使用它为我工作:

public void slideAnimation(RecyclerView.ViewHolder holder, boolean animate) {
    ObjectAnimator animatorY = ObjectAnimator.ofFloat(holder.itemView, "translationY", animate ? 40 : -40, 1f);
    ObjectAnimator animatorX = ObjectAnimator.ofFloat(holder.itemView, "translationX", animate ? 40 : -40, 1f);
    AnimatorSet set = new AnimatorSet();
    set.playTogether(animatorX , animatorY);
    set.setDuration(1000);
    set.start();

}

【讨论】:

  • 嘿,感谢您的回复,它不起作用。您在 recyclerview 中使用哪个 itemview,recyclerview 中的项目数量是否有最小限制
  • 我在帖子中添加了 onBindViewHolder() 方法
【解决方案3】:

尝试将行布局父根 xml 标记的引用保留到 holder 中并使用它而不是使用 holder.itemview。

【讨论】:

    【解决方案4】:

    抱歉,我没有在我的设备中启用动画。在开发人员选项中将窗口动画缩放、过渡动画缩放、动画器持续时间缩放为 1 倍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多