ViewGroup | Android Developers
onInterceptTouchEvent
added in API level 1
public boolean onInterceptTouchEvent (MotionEvent ev)
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:
You will receive the down event here.
The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
If you return true from here, you will not receive any following events: the target view will receive the same event but with the action MotionEvent.ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
翻译一下吧!
继承这个方法用于拦截所有的屏幕触摸事件。允许监测所有被分发到子控件的事件,以及掌控手势的所有权。
用这个方法需要注意,它与View.onTouchEvent(MotionEvent)有个相对复杂的互相影响,所以需要正确的实现该方法以及使用它。事件接收顺序如下:
1. 首先你会接收到down(按下事件)事件
2. Down事件将会被该ViewGroup的一个子控件所处理,或者ViewGroup自己的onTouchEvent处理。这就意味着ViewGroup需要实现onTouchEvent并且返回true。接下来就可以在onTouchEvent中处理剩下的手势了。
3. 如果该函数(onInterceptTouchEvent)返回false。接下来的每个手势都会首先派发到这里,然后派发到目标的onTouchEvent()中。
4.如果该函数返回true,你将不会收到任何的响应事件。目标的View只能接收到MotionEvent.ACTION_CANCEL 的响应,所有后来的事件都将直接传递到onTouchEvent中,不会出现在这个方法(onInterceptTouchEvent)中。
总得来说还是好理解。我之前在准备做这个翻译理解的时候,完全不懂,后来我是先做了些实际的实践,打印,简单需求模拟。之后再来看这个,相对要简单一些了。Android-自定义View的事件分发及拦截-父控件和子控件都处理触摸事件的方式
onTouchEvent
public boolean onTouchEvent (MotionEvent event)
Implement this method to handle touch screen motion events.
If this method is used to detect click actions, it is recommended that the actions be
performed by implementing and calling performClick(). This will ensure consistent system
behavior, including:
obeying click sound preferences
dispatching OnClickListener calls
handling ACTION_CLICK when accessibility features are enabled
Parameters
event MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.
这个也来翻译下:简单理解就是处理屏幕触摸事件的。如果这个方法被用来检测点击动作,推荐实现以及调用performClick方法。这样能保证系统的行为:包括点击声音,当一些特性可以用时,分派点击事件回调处理ACTION_CLICK。 当事件被处理返回true,否则返回false。
这个倒是没什么特别的详细,还是得结合实践了解。关注下performClick....
抄袭下网友的简单说明,觉得人家不错的给人家点个赞...
image其实我觉得还是自己去实践过会能深得一些些体会,不然一开始可能会比较难理解。小白就有这样的感受....(有些具体的深入,小白目前暂时还没有深入太多。等自定义View入门的路打通了是需要深入的)
performClick
performClick
added in API level 1
public boolean performClick ()
Call this view's OnClickListener, if it is defined. Performs all normal actions associated with
clicking: reporting accessibility event, playing a sound, etc.
Returns
boolean True there was an assigned OnClickListener that was called, false otherwise is returned.
来一篇解释,小白觉得还行 performClick - 皮卡丘的博客 - CSDN博客
后面综合练习的时候应该会遇到....
接着我们看一个接口ViewParent,其中的一个方法 ViewParent | Android Developers
requestDisallowInterceptTouchEvent
Called when a child does not want this parent and its ancestors to intercept touch events with
ViewGroup.onInterceptTouchEvent(MotionEvent).
This parent should pass this call onto its parents. This parent must obey this request for
the duration of the touch (that is, only clear the flag after this parent has received an up
or a cancel.
Parameters
disallowIntercept boolean: True if the child does not want the parent to intercept
touch events.
解释:大概意思就是如果设置为true, 子控件不希望父亲在onInterceptTouchEvent进行事件拦截。
那是不是我们上一篇文章当中,如果子控件在进行上下滑动的时候不是ViewGroup会上下滑动(也就是会在onInterceptTouchEvent中返回true),而子控件不做处理么。 那我们是不是可以通过调用这个方法告诉ViewGroup不要拦截上下滑动。
试试呗...Android-自定义ViewGroup-上下滑动整体实践下
CustomTextView.java - getParent().requestDisallowInterceptTouchEvent(true);
private float x1, x2;
private float y1, y2;
private float swing = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("event", "CustomTextView onTouchEvent" + event.getAction());
//return super.onTouchEvent(event);
getParent().requestDisallowInterceptTouchEvent(true);
.......
}
然后我们发现当滑动子控件时怎么都不会引起ViewGroup进行上下滑动了...然后目前来看貌似用不到的样子。但是有些事件冲突应该是会有用途的。后面要针对学习下......
好的,小白到这里提到这些个东东。。。。继续小白之路..
心灵鸡汤 - 不要怕,活着就好,心情要好,要早睡早起,心情好什么都好,一起做生活的主人吧, 小白骚年们 - 黄磊
网友评论