/** * @param 水平滚动的像素值 * @param 垂直滚动的距离像素值 * @param consumed[0] :dx消耗掉的部分 * consumed[1] dy消费的部分 * @param offsetInWindow Optional. If not null, on return this will contain the offset * in local view coordinates of this view from before this operation * to after it completes. View implementations may use this to adjust * expected input coordinate tracking. * @return 如果父view消费了一些或全部,则返回true */ publicbooleandispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);
@Override publicbooleanonTouchEvent(MotionEvent event){ switch (event.getAction()) { case MotionEvent.ACTION_DOWN: ox = event.getX(); oy = event.getY(); //开始滑动!!!!!!!!!! startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL); break; case MotionEvent.ACTION_MOVE://在MOVE中 //结束点 float clampedX = event.getX(); float clampedY = event.getY(); //移动的距离 int dx = (int) (clampedX - ox); int dy = (int) (clampedY - oy); //分发触屏事件给父类处理 if (dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)) { //减掉父类消耗的距离 dx -= consumed[0];//consumed[0] will contain the consumed component of dx dy -= consumed[1];//consumed[1] the consumed dy } offsetLeftAndRight(dx);//Offset this view's horizontal location by the specified amount of pixels offsetTopAndBottom(dy);//通过指定像素来弥补控件的垂直位置 break; case MotionEvent.ACTION_UP: stopNestedScroll();//结束滑动!!!!!!!! break; } returntrue;/*super.onTouchEvent(event);*/ }
1 2 3 4 5 6 7 8 9
/** *子类滑动事件分发回调 * @param target View that initiated the nested scroll * @param dx Horizontal scroll distance in pixels * @param dy Vertical scroll distance in pixels * @param consumed Output. The horizontal and vertical scroll distance consumed by this parent */ publicvoidonNestedPreScroll(View target, int dx, int dy, int[] consumed);