我确实扩展了NestedScrollView和@Override方法: onTouchEvent 和 onInterceptTouchEvent 。问题是super.onTouchEvent从不调用ACTION_DOWN(它由我的onIntereceptTouchEvent调用),因为NestedScrollView没有移动,我得到错误:
onTouchEvent中的pointerId = -1无效
当触发ACTION_DOWN时,指针id在NestedScrollView的onTouchEvent方法中设置。
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
我自己无法设置,重写整个onTouchEvent方法似乎是痛苦的计划。可以在这里使用帮助。当我单击不监听触摸事件的View时,NestedScrollView会移动(基本上,我的onInterecptTouchEvent方法没有被调用,而且在NestedScrollView onTouchEvent中处理了ACTION_DOWN)。
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i(LOGTAG, String.valueOf(ev.getAction()));
final int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
mIsBeingDragged = false;
Log.i(LOGTAG, "is scrolling" + String.valueOf(mIsBeingDragged));
return false; // Do not intercept touch event, let the child handle it
}
switch (action) {
case MotionEvent.ACTION_DOWN: {
mStartY = ev.getY();
break;
}
case MotionEvent.ACTION_MOVE: {
if (mIsBeingDragged) {
return true;
}
final int yDiff = calculateDistanceY(ev);
Log.d(LOGTAG, "y difference: y");
if (yDiff > mTouchSlop) {
mIsBeingDragged = true;
return true;
}
break;
}
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
Log.i(LOGTAG, "onTouchEvent");
if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
mIsBeingDragged = false;
}
return super.onTouchEvent(ev);
}
答案 0 :(得分:3)
以下是修复我的问题的代码,我不得不撒谎:
@Override
public boolean onTouchEvent(MotionEvent ev) {
Log.i(LOGTAG, "onTouchEvent");
if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
mIsBeingDragged = false;
}
if (mFirstOnTouchEvent) {
ev.setAction(MotionEvent.ACTION_DOWN);
ev.setLocation(mStartX, mStartY);
mFirstOnTouchEvent = false;
}
return super.onTouchEvent(ev);
}
并在onInterceptTouchEvent中触发ACTION_DOWN时将mFirstOnTouchEvent设置为true