@Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) { // Don't handle edge touches immediately -- they may actually belong to one of our // descendants. return false; }
if (!canScroll()) { return false; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ if (!mScroller.isFinished()) { mScroller.abortAnimation(); } // Remember where the motion event started mLastMotionY = y; break; case MotionEvent.ACTION_MOVE: // Scroll to follow the motion event final int deltaY = (int) (mLastMotionY - y); mLastMotionY = y; if (deltaY < 0) { if (mScrollY > 0) { scrollBy(0, deltaY); } } else if (deltaY > 0) { final int bottomEdge = getHeight() - mPaddingBottom; final int availableToScroll = getChildAt(0).getBottom() - mScrollY - bottomEdge; if (availableToScroll > 0) { scrollBy(0, Math.min(availableToScroll, deltaY)); } } break; case MotionEvent.ACTION_UP: final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) velocityTracker.getYVelocity(); if ((Math.abs(initialVelocity) > mMinimumVelocity) && getChildCount() > 0) { fling(-initialVelocity); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } } return true; }这是scrollview 中的 onTouchEvent 源码。 LZ可以在判断为 ACTION_MOVE 时 返回 true,再其他action时返回false 让上一层去处理这个操作。
public boolean dispatchTouchEvent (MotionEvent event) Pass the touch screen motion event down to the target view, or this view if it is the target. Parameters View的dispatchTouchEvent是可以直接分派触摸事件到指定的view上的 手册上没说是向下分派,可以试试 我没验证过哈
你要的效果逻辑已经很清楚了,上下滑动你处理scrollview,左右滑你处理Gallery就好了,都返回true
public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
// Don't handle edge touches immediately -- they may actually belong to one of our
// descendants.
return false;
}
if (!canScroll()) {
return false;
} if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev); final int action = ev.getAction();
final float y = ev.getY(); switch (action) {
case MotionEvent.ACTION_DOWN:
/*
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
} // Remember where the motion event started
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
// Scroll to follow the motion event
final int deltaY = (int) (mLastMotionY - y);
mLastMotionY = y; if (deltaY < 0) {
if (mScrollY > 0) {
scrollBy(0, deltaY);
}
} else if (deltaY > 0) {
final int bottomEdge = getHeight() - mPaddingBottom;
final int availableToScroll = getChildAt(0).getBottom() - mScrollY - bottomEdge;
if (availableToScroll > 0) {
scrollBy(0, Math.min(availableToScroll, deltaY));
}
}
break;
case MotionEvent.ACTION_UP:
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) velocityTracker.getYVelocity(); if ((Math.abs(initialVelocity) > mMinimumVelocity) && getChildCount() > 0) {
fling(-initialVelocity);
} if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
return true;
}这是scrollview 中的 onTouchEvent 源码。 LZ可以在判断为 ACTION_MOVE 时 返回 true,再其他action时返回false 让上一层去处理这个操作。
public boolean dispatchTouchEvent (MotionEvent event)
Pass the touch screen motion event down to the target view, or this view if it is the target.
Parameters
View的dispatchTouchEvent是可以直接分派触摸事件到指定的view上的 手册上没说是向下分派,可以试试 我没验证过哈
不用那么麻烦吧?你只用判断scrollview 里接受的动作是不是 上下滑动,如果是就consume 掉,如果不是就return false。
哥,你可以不对ACTION_DOWN这个操作进行判断三。
判断的条件只有一个,是否进行了ACTION_MOVE操作
如果想捕获多个操作,只能在两个不属于包含关系的view中分别进行处理。