美文网首页
handler内存泄漏解决方式

handler内存泄漏解决方式

作者: 游侠_6fb7 | 来源:发表于2020-07-07 15:12 被阅读0次
    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();
            }
    
        }
    

    相关文章

      网友评论

          本文标题:handler内存泄漏解决方式

          本文链接:https://www.haomeiwen.com/subject/kjxtnhtx.html