@Override
  public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        downX = tempX = (int) event.getRawX();
        downY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_MOVE:
        int moveX = (int) event.getRawX();
        int deltaX = tempX - moveX;
        tempX = moveX;
        if (Math.abs(moveX - downX) > mTouchSlop
            && Math.abs((int) event.getRawY() - downY) < mTouchSlop) {
          isSilding = true;

          if (isTouchOnAbsListView()) {
            MotionEvent cancelEvent = MotionEvent.obtain(event);
            cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL
                    | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            v.onTouchEvent(cancelEvent);
          }
        }

        if (moveX - downX >= 0 && isSilding) {
          mParentView.scrollBy(deltaX, 0);

          if (isTouchOnScrollView() || isTouchOnAbsListView()) {
            return true;
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        isSilding = false;
        if (mParentView.getScrollX() <= -viewWidth / 2) {
          isFinish = true;
          scrollRight();
        } else {
          scrollOrigin();
          isFinish = false;
        }
        break;
    }

    if (isTouchOnScrollView() || isTouchOnAbsListView()) {
      return v.onTouchEvent(event);
    }

    return true;
  }
  private DropTarget findDropTarget(ViewGroup container, int x, int y, int[] dropCoordinates) {
    final Rect r = mDragRect;
    final int count = container.getChildCount();
    final int scrolledX = x + container.getScrollX();
    final int scrolledY = y + container.getScrollY();
    final View ignoredDropTarget = mIgnoredDropTarget;

    for (int i = count - 1; i >= 0; i--) {
      final View child = container.getChildAt(i);
      if (child.getVisibility() == VISIBLE && child != ignoredDropTarget) {
        child.getHitRect(r);
        if (r.contains(scrolledX, scrolledY)) {
          DropTarget target = null;
          if (child instanceof ViewGroup) {
            x = scrolledX - child.getLeft();
            y = scrolledY - child.getTop();
            target = findDropTarget((ViewGroup) child, x, y, dropCoordinates);
          }
          if (target == null) {
            if (child instanceof DropTarget) {
              // Only consider this child if they will accept
              DropTarget childTarget = (DropTarget) child;
              if (childTarget.acceptDrop(mDragSource, x, y, 0, 0, mDragInfo)) {
                dropCoordinates[0] = x;
                dropCoordinates[1] = y;
                return (DropTarget) child;
              } else {
                return null;
              }
            }
          } else {
            return target;
          }
        }
      }
    }

    return null;
  }
Пример #3
0
  public static boolean isTransformedPointInView(
      ViewGroup parent, View child, float x, float y, @Nullable PointF outLocalPoint) {
    Util.throwIfNull(parent);
    Util.throwIfNull(child);

    float localX = x + parent.getScrollX() - child.getLeft();
    float localY = y + parent.getScrollY() - child.getTop();

    // TODO: handle transforms
    /*Matrix childMatrix = child.getMatrix();
    if (!childMatrix.isIdentity()) {
      final float[] localXY = new float[2];
      localXY[0] = localX;
      localXY[1] = localY;
      child.getInverseMatrix
    }*/

    final boolean isInView = ViewUtil.pointInView(child, localX, localY);
    if (isInView && outLocalPoint != null) {
      outLocalPoint.set(localX, localY);
    }

    return isInView;
  }
 private void scrollOrigin() {
   int delta = mParentView.getScrollX();
   mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0, Math.abs(delta));
   postInvalidate();
 }
  private void scrollRight() {
    final int delta = (viewWidth + mParentView.getScrollX());

    mScroller.startScroll(mParentView.getScrollX(), 0, -delta + 1, 0, Math.abs(delta));
    postInvalidate();
  }