嵌套滚动视图和 onInterceptTouchEvent
NestedScrollView and onInterceptTouchEvent
我确实扩展了 NestedScrollView 和 @Override 它的方法:onTouchEvent 和 onInterceptTouchEvent。问题是 super.onTouchEvent 从不调用 ACTION_DOWN (它由我的 onIntereceptTouchEvent 调用)并且因此 NestedScrollView 不会移动并且我得到错误:
Invalid pointerId=-1 in onTouchEvent
指针id在ACTION_DOWN触发时在NestedScrollView的onTouchEvent方法中设置。
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
我不能自己设置这个,重写整个 onTouchEvent 方法似乎是一个痛苦的计划。可以在这里使用帮助。当我单击不侦听触摸事件的视图时,NestedScrollView 确实移动了(基本上这样我的 onInterecptTouchEvent 方法就不会被调用并且 ACTION_DOWN 在 NestedScrollView onTouchEvent 中处理)。
@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);
}
下面是解决我的问题的代码,我不得不撒谎:
@Override
public boolean onTouchEvent(MotionEvent ev) {
Log.i(LOGTAG, "onTouchEvent");
if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
mIsBeingDragged = true;
}
if (mFirstOnTouchEvent) {
ev.setAction(MotionEvent.ACTION_DOWN);
ev.setLocation(mStartX, mStartY);
mFirstOnTouchEvent = false;
}
return super.onTouchEvent(ev);
}
并在onInterceptTouchEvent中触发ACTION_DOWN时将mFirstOnTouchEvent设置为true:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (MotionEventCompat.getActionMasked(ev) == MotionEvent.ACTION_DOWN) {
mFirstOnTouchEvent = true
}
...
}
我确实扩展了 NestedScrollView 和 @Override 它的方法:onTouchEvent 和 onInterceptTouchEvent。问题是 super.onTouchEvent 从不调用 ACTION_DOWN (它由我的 onIntereceptTouchEvent 调用)并且因此 NestedScrollView 不会移动并且我得到错误:
Invalid pointerId=-1 in onTouchEvent
指针id在ACTION_DOWN触发时在NestedScrollView的onTouchEvent方法中设置。
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
我不能自己设置这个,重写整个 onTouchEvent 方法似乎是一个痛苦的计划。可以在这里使用帮助。当我单击不侦听触摸事件的视图时,NestedScrollView 确实移动了(基本上这样我的 onInterecptTouchEvent 方法就不会被调用并且 ACTION_DOWN 在 NestedScrollView onTouchEvent 中处理)。
@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);
}
下面是解决我的问题的代码,我不得不撒谎:
@Override
public boolean onTouchEvent(MotionEvent ev) {
Log.i(LOGTAG, "onTouchEvent");
if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
mIsBeingDragged = true;
}
if (mFirstOnTouchEvent) {
ev.setAction(MotionEvent.ACTION_DOWN);
ev.setLocation(mStartX, mStartY);
mFirstOnTouchEvent = false;
}
return super.onTouchEvent(ev);
}
并在onInterceptTouchEvent中触发ACTION_DOWN时将mFirstOnTouchEvent设置为true:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (MotionEventCompat.getActionMasked(ev) == MotionEvent.ACTION_DOWN) {
mFirstOnTouchEvent = true
}
...
}