我有一个RecyclerView,其中RecyclerView.ViewHolder由以下自定义视图支持。例如:
public class ItemView extends View {
private float mSpeed = 0;
private ValueAnimator mValueAnimator;
/* View constructor omitted for brevity */
/**
* Sets and animates from last known mSpeed to current speed
* @param speed current speed
*/
public void setSpeed(float speed){
mValueAnimator = ValueAnimator.ofFloat(mSpeed, speed);
mValueAnimator.addUpdateListener(valueAnimator -> {
mSpeed = speed;
invalidate();
});
mValueAnimator.start();
}
}
我需要View保持其mSpeed状态才能制作动画。当我调用RecyclerView.Adapter的notifyItemChanged(position)时,将重新创建视图,并且状态丢失。这导致mSpeed始终为0的不良行为。
我正在使用的模型只有当前的速度,我打算接触模型类,但是我真的不想这么做。例如:
public class Model {
private float mSpeed;
public float getSpeed(){
return mSpeed;
}
}