Android 禁用鼠标滚轮(一)

在 Android 9 中调用 ListView 时,概率性会导致 界面UI布局乱掉,出现异常.

第一种:修改UI布局

第二种:当鼠标操作为低概率事件时,可以禁用鼠标滚轮实现该功能

若要禁用 鼠标滚轮 ,可以在Android源码中拦截事件分发,也可以在应用中拦截滚轮事件.

/**
     * Called when a generic motion event was not handled by any of the
     * views inside of the activity.
     * 

* Generic motion events describe joystick movements, mouse hovers, track pad * touches, scroll wheel movements and other input events. The * {@link MotionEvent#getSource() source} of the motion event specifies * the class of input that was received. Implementations of this method * must examine the bits in the source before processing the event. * The following code example shows how this is done. *

* Generic motion events with source class * {@link android.view.InputDevice#SOURCE_CLASS_POINTER} * are delivered to the view under the pointer. All other generic motion events are * delivered to the focused view. *

* See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to * handle this event. *

* * @param event The generic motion event being processed. * * @return Return true if you have consumed the event, false if you haven't. * The default implementation always returns false. */ public boolean onGenericMotionEvent(MotionEvent event) { return false; }

此方法为获取鼠标滚轮信息,可打印 event 相关信息,此处已设置为false.

继而往上追,追到父类中  dispatchGenericMotionEvent(MotionEvent ev) ,代码如下

/**
     * Called to process generic motion events.  You can override this to
     * intercept all generic motion events before they are dispatched to the
     * window.  Be sure to call this implementation for generic motion events
     * that should be handled normally.
     *
     * @param ev The generic motion event.
     *
     * @return boolean Return true if this event was consumed.
     */
    public boolean dispatchGenericMotionEvent(MotionEvent ev) {
        onUserInteraction();
        if (getWindow().superDispatchGenericMotionEvent(ev)) {
            return true;
        }
        return onGenericMotionEvent(ev);
    }

查看代码后想,若是重写dispatchGenericMotionEvent()此方法,不去进行判断鼠标滚轮事件,直接return false ,是否可行.

@Override
	// Shield mouse wheel
	public boolean dispatchGenericMotionEvent(MotionEvent ev) {
		return false;
	}

经测试验证,此方法可行.关于在系统framework中的拦截按键,后续学习整理后上传
 

你可能感兴趣的:(Android,O,android,ui,java)