问题
I have a custom ViewGroup with an override of onInterceptTouchEvent(). It receives ACTION_DOWN but never receives ACTION_MOVE. It is my understanding that, unless it returns "true", it should receive all MotionEvents.
The ViewGroup contains two views, an ImageView and a GridLayout.
My intercept code is:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
logD ("DDV Intercept DOWN");
break;
case MotionEvent.ACTION_POINTER_DOWN:
logD ("DDV Intercept P DOWN"); // logD: shell around Log.d()
break;
case MotionEvent.ACTION_MOVE:
logD ("DDV Intercept MOVE");
break;
case MotionEvent.ACTION_UP:
logD ("DDV Intercept UP");
break;
case MotionEvent.ACTION_POINTER_UP:
logD ("DDV Intercept P UP " + ev.getActionIndex());
break;
case MotionEvent.ACTION_CANCEL:
logD ("DDV Intercept CANCEL");
break;
default:
logD ("DDV Intercept " + (action & MotionEvent.ACTION_MASK));
}
return false;
}
I also have code for onTouch that returns false except for one case in ACTION_MOVE; however, it's called only for ACTION_DOWN is called; thus it only return false.
回答1:
It's a bit more complicated than that. First of all you need to override onTouchEvent()
and handle ACTION_DOWN
and MOVE
events there too. Then the following will happen.
ACTION_DOWN
event gets dispatched toonInterceptTouchEvent()
first. You should returnfalse
from there.- Now there are two cases:
- If there is no touchable view underneath
ACTION_DONW
event's location in the view tree, thenACTION_DOWN
event and all follow up events get dispatched toonTouchEvent()
. You must returntrue
from there. Only then you will receive follow up events sent toonTouchEvent()
method. Independently on whether you returntrue
orfalse
,onInterceptTouchEvent()
will not receive any follow up events anymore. - If there is a touchable view, then all events will be dispatched to
onInterceptTouchEvent()
(includingACTION_MOVE
events). You need to returntrue
from there, after you detected your gesture. Once you returntrue
from here, touchable view will receiveACTION_CANCEL
event and all further events will be dispatched toonTouchEvent()
method.
- If there is no touchable view underneath
Hope this helps.
来源:https://stackoverflow.com/questions/23725102/onintercepttouchevent-never-receives-action-move