public abstract class MyHandler<T> extends Handler {
private WeakReference<T> mTargets;
public MyHandler(T target) {
mTargets = new WeakReference<>(target);
}
public MyHandler(T target, Looper looper) {
super(looper);
mTargets = new WeakReference<>(target);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
T target = mTargets.get();
if (target != null) {
handle(target, msg);
}
}
public abstract void handle(T target, Message msg);
}
private static class PickerViewHandler extends MyHandler<MyPickerView> {
PickerViewHandler(MyPickerView target) {
super(target);
}
@Override
public void handle(MyPickerView target, android.os.Message msg) {
if (Math.abs(target.mMoveLen) < SPEED) {
target.mMoveLen = 0;
if (target.mTask != null && !target.isDataListEmpty()) {
target.mTask.cancel();
target.mTask = null;
target.performSelect();
}
} else {
target.mMoveLen = target.mMoveLen - target.mMoveLen / Math.abs(target.mMoveLen) * SPEED;
}
target.invalidate();
}
}
网友评论