展会信息港展会大全

浅谈onInterceptTouchEvent、onTouchEvent与onTouch
来源:互联网   发布日期:2015-10-02 16:07:55   浏览:1447次  

导读:一、onTouchonTouch是View中OnTouchListener接口中的方法,处理View及其子类被touch是的事件处理。当然,前提是touch时间能够传递到指定的view。Q1:为什么会传递不到呢? /** * Interface definition......

一、onTouch

onTouch是View中OnTouchListener接口中的方法,处理View及其子类被touch是的事件处理。当然,前提是touch时间能够传递到指定的view。Q1:为什么会传递不到呢?

/**

* Interface definition for a callback to be invoked when a touch event is

* dispatched to this view. The callback will be invoked before the touch

* event is given to the view.

*/

public interface OnTouchListener {

/**

* Called when a touch event is dispatched to a view. This allows listeners to

* get a chance to respond before the target view.

*

* @param v The view the touch event has been dispatched to.

* @param event The MotionEvent object containing full information about

*the event.

* @return True if the listener has consumed the event, false otherwise.

*/

boolean onTouch(View v, MotionEvent event);

}

二、onTouchEvent

onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四种事件。

/**

* Implement this method to handle touch screen motion events.

*

* @param event The motion event.

* @return True if the event was handled, false otherwise.

*/

public boolean onTouchEvent(MotionEvent event) {

}

一旦onTouchEvent方法被调用,并返回true则这个手势事件就结束了,并不会向下传递到子控件。Q2:onTouchEvent什么时候被调用呢?

三、onInterceptTouchEvent

onInterceptTouchEvent是在ViewGroup里面定义的。Android中的layout布局类一般都是继承此类的。 onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent。

public boolean onInterceptTouchEvent(MotionEvent ev) {

return false;

}

此方法返回false,则手势事件会向子控件传递;返回true,则调用onTouchEvent方法。

赞助本站

人工智能实验室
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港