在给item加上动画之后,慢速滑动的时候是没有问题的,但要快速滑动就会出现卡屏现象。
public abstract class BaseAnimAdapter extends BaseAdapter {
private int lastPosition = -1;
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
setAnimation(holder.itemView, position);
}
protected void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), R.anim.item_slide_bottom_up);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
问题就出在动画设置完之后,没有及时清除动画,添加以下代码即可:
public abstract class BaseAnimAdapter extends BaseAdapter {
private int lastPosition = -1;
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
setAnimation(holder.itemView, position);
}
protected void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), R.anim.item_slide_bottom_up);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.itemView.clearAnimation();
}
}
时间: 2024-10-21 07:29:00