在android中,有时候会遇到子控件和父控件都要滑动的状况,尤为是当子控件为listview的时候。这种状况较常见,典型的launcher,每一个屏幕上放上listview就会出现这种状况。java
有两点须要注意:android
其实解决方法也很简单:重写父控件的onInterceptTouchEvent函数,在move的时候根据须要返回true,好比左右滑动返回true,其余状况均返回false。这样,当左右滑动的时候,因为onInterceptTouchEvent返回了true,父控件就能处理,其余状况,事件将传递到listview中,listview自身能够处理上下滑动。ide
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { Log.d(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop); final int action = ev.getAction(); if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { return true; } final float x = ev.getX(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_MOVE: final int xDiff = (int)Math.abs(mLastMotionX-x); if (xDiff>mTouchSlop) { mTouchState = TOUCH_STATE_SCROLLING; } break; case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: mTouchState = TOUCH_STATE_REST; break; } return mTouchState != TOUCH_STATE_REST; }