handler内存泄漏解决方式

public abstract class MyHandler extends Handler {

    private WeakReference 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 {

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

    }

你可能感兴趣的:(handler内存泄漏解决方式)