我确实扩展了nestedscrollview并@override它的方法:ontouchevent和onintercepttouchevent。问题是super.ontouchevent从不调用action_down(它是由我的onIntereceptouchevent调用的),并且由于嵌套的ScrollView不移动,我得到错误:
ontouchevent中的pointerid=-1无效
当触发action_down时,指针id在nestedscrollview的ontouchevent方法中设置。
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
我自己无法设定,重写整个本体论的方法似乎是痛苦的计划。这里需要帮助。当我单击不侦听触摸事件的视图时,NestedScrollView确实会移动(基本上,这样就不会调用我的OnInterecptTouchEvent方法,并且在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 = false;
}
if (mFirstOnTouchEvent) {
ev.setAction(MotionEvent.ACTION_DOWN);
ev.setLocation(mStartX, mStartY);
mFirstOnTouchEvent = false;
}
return super.onTouchEvent(ev);
}
并在IntercepttouchEvent中触发操作时将mfirstontouchEvent设置为true
关于android - NestedScrollView和onInterceptTouchEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32699268/