我有两个视图:子视图和父视图(子视图是父视图的一部分);每个视图都有自己的onTouchListener。我希望每个视图都能处理自己的onInterceptTouchEvent。马上;但是,子视图的onTouchListener永远不会收到任何事件。他们都被发送到父视图的听众。如何确保子视图收到自己的事件?
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Log.d(TAG, "onInterceptTouchEvent " + ev);
// handle child events
// note: if you horizontally fling over button its onClick() is not performed
MotionEvent ss = MotionEvent.obtain(ev);
globalVariabels.currentGallery.onTouchEvent(ss);
int bounds[] = {0,0};
globalVariabels.currentGallery.getLocationOnScreen(bounds);
int height = globalVariabels.currentGallery.getHeight() + bounds[0];
int width = globalVariabels.currentGallery.getWidth() + bounds[1];
TouchDelegate delegate = new TouchDelegate(new Rect(0, 0, 2000, 2000), globalVariabels.currentGallery);
setTouchDelegate(delegate);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
mIgnore = false;
Log.e("ooo", "oo");
Log.e("X:","" + ev.getX());
Log.e("Y:","" + ev.getY());
mNeedToRebase = true;
mInitialX = ev.getX();
mInitialY = ev.getY();
return false ;
}
case MotionEvent.ACTION_MOVE: {
if (!mIgnore) {
Log.e("22", "22");
float deltaX = Math.abs(ev.getX() - mInitialX);
float deltaY = Math.abs(ev.getY() - mInitialY);
mIgnore = deltaX < deltaY;
super.onInterceptTouchEvent(ev);
return !mIgnore;
}
return false;
}
default: {
return super.onInterceptTouchEvent(ev);
}
}
}
答案 0 :(得分:0)
考虑使用dispatchTouchEvent
代替onInterceptTouchEvent
。我遇到了一些问题,因为在某些设备上(在三星上,更精确)它可能不会像预期的那样工作。调度真的很简单。
想法是你将覆盖dispatchTouchEvent
,调用超级并处理你的动作事件。这是一个例子:
@Override
protected boolean dispatchTouchEvent(MotionEvent ev) {
//perform your on-touch behaviour
return super.dispatchTouchEvent(ev); //child's onTouchListener will be triggered
}