安卓Android自定义View

用两段代码带你看懂事件分发机制

2020-12-19  本文已影响0人  Android轮子哥

先来看一段精简版的源码

public class View {

    private View.OnClickListener mOnClickListener;
    private OnLongClickListener mOnLongClickListener;
    private OnTouchListener mOnTouchListener;

    public boolean dispatchTouchEvent(MotionEvent event) {
        boolean result = false;
        if (isEnabled()) {
            if (mOnTouchListener != null &&
                    mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

            if (!result && onTouchEvent(event)) {
                result = true;
            }
        }
        return result;
    }

    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        if (isClickable()) {
            switch (action) {
                case MotionEvent.ACTION_UP:
                    performClick();
                    break;
                case MotionEvent.ACTION_DOWN:
                    performLongClick();
                    break;
                default:
                    break;
            }
            return true;
        }
        return false;
    }

    public boolean performClick() {
        final boolean result;
        if (mOnClickListener != null) {
            mOnClickListener.onClick(this);
            result = true;
        } else {
            result = false;
        }
        return result;
    }

    public boolean performLongClick() {
        boolean handled = false;
        if (mOnLongClickListener != null) {
            handled = mOnLongClickListener.onLongClick(this);
        }
        return handled;
    }

    public void setOnClickListener(OnClickListener listener) {
        mOnClickListener = listener;
    }

    public void setOnLongClickListener(OnLongClickListener listener) {
        mOnLongClickListener = listener;
    }

    public void setOnTouchListener(OnTouchListener listener) {
        mOnTouchListener = listener;
    }
}
public class ViewGroup extends View {

    private boolean mDisallowIntercept;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        boolean handled;
        final int action = event.getAction();

        boolean intercepted = false;
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (!mDisallowIntercept) {
                    intercepted = onInterceptTouchEvent(event);
                }
                break;
            default:
                break;
        }

        if (!intercepted) {
            View childView = findFocus();
            handled = childView.dispatchTouchEvent(event);
        } else {
            handled = super.dispatchTouchEvent(event);
        }
        return handled;
    }

    public boolean onInterceptTouchEvent(MotionEvent event) {
        return false;
    }

    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        mDisallowIntercept = disallowIntercept;
    }
}

主要方法解释

其他方法解释

上一篇 下一篇

猜你喜欢

热点阅读