RecyclerView 添加动画,以及快速滑动导致的问题

RecyclerView 添加动画,以及快速滑动导致的问题

这个是添加动画在itemView展示的时候

 @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        setAnimation(position,holder);
    }

添加动画的方法

public void setAnimation(int position,ViewHolder holder){
        Animation animation = AnimationUtils.loadAnimation(context,
                (position > lastPosition) ? R.anim.up_from_bottom
                        : R.anim.down_from_top);
        holder.itemView.startAnimation(animation);
        lastPosition = position;
    }

down_from_top


<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="-100%" android:toYDelta="0%"
        android:duration="400" />
set>

up_from_bottom


<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="400" />
set>
/**
     * 当itemView滑出界面外调用,停止动画,这样就解决了快速滑动导致的问题
     * @param holder
     */
    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

你可能感兴趣的:(Android,android-面试)