Ahmad Nasser Ahmad Nasser - 9 months ago 40
Java Question

Android Recycleview animation

I have recycleview and i need to apply animation on the list items here is the adapter and the problem is that the rows animate but its not work fine some times overlap each others and sometime make blinking any suggestion will be welcome

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();
}
}


and here is the animation resource

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

Answer Source

Clear the animation in onViewDetachedFromWindow method

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

}