Android Recycleview动画

时间:2017-03-12 07:36:33

标签: java android xml animation

我有循环视图,我需要在列表项目上应用动画这里是适配器,问题是行动画但它的工作不正常有时相互重叠,有时闪烁任何建议都欢迎

public class ActionsAdapter extends            RecyclerView.Adapter<ActionsAdapter.MyViewHolder> {

private Context context;
private List<Notification> notifications;
private int lastPosition=-1;

public void setNotifications(List<Notification> notifications) {
    if (notifications == null) {
        this.notifications = new ArrayList<>();
    } else {
        this.notifications = notifications;
    }
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView notificaytionText;
    TextView notificationStatus;

    ImageView notificationIndecator;
    View view;

    public MyViewHolder(View view) {
        super(view);
        this.view = view;
        notificaytionText = (TextView) view.findViewById(R.id.notification_text);
        notificationStatus = (TextView) view.findViewById(R.id.notification_status);
        notificationIndecator = (ImageView) view.findViewById(R.id.notification_indecator);
    }
}

public List<Notification> getNotifications() {
    return notifications;
}

public ActionsAdapter(Context context) {
    this.context = context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_actions, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.notificaytionText.setText(notifications.get(position).getSubmittedBy());
    Utils.setNotificationStatusColor(context, holder.notificationStatus, notifications.get(position).getStatusCode());
    if (Utils.isArabicLanguage()) {
        holder.notificaytionText.setGravity(Gravity.RIGHT);
        holder.notificationIndecator.setRotation(180);
    }
    FontManager.setViewRebotoFont(context,holder.notificaytionText,FONTS.REGULAR,0);
    if (position > lastPosition) {

        Animation animation = AnimationUtils.loadAnimation(context,
                R.anim.recycle_from_right
        );
        holder.itemView.startAnimation(animation);
        lastPosition = position;
    }
}

@Override
public int getItemCount() {
    return notifications.size();
}
  }

这是动画资源

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
 <translate
    android:fromXDelta="100%" android:toXDelta="0%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="900" />
 </set>

1 个答案:

答案 0 :(得分:1)

在onViewDetachedFromWindow方法中清除动画

@Override
public void onViewDetachedFromWindow(ViewHolder holder) {
   holder.itemView.clearAnimation();

}