滑动不在scrollview中工作

时间:2016-01-05 09:33:50

标签: android scrollview gestures

我正在使用此calss来检测滑动手势,除了使用scrollView之外它工作正常。滚动视图或其子项吸收我的滑动因此无法捕捉它们。

在我的活动中

// Swipe
    view = (View) findViewById(R.id.layoutClean);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        }
    });
    view.setOnTouchListener(new OnSwipeTouchListener() {
        public boolean onSwipeRight() {
            Intent intent = new Intent(Clean.this, Tools.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_left_to_right_1,
                    R.anim.slide_left_to_right_2);
            return true;
        }

        public boolean onSwipeLeft() {
            Intent intent = new Intent(Clean.this, Boost.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_right_to_left_1,
                    R.anim.slide_right_to_left_2);
            return true;
        }
    });
    // Swipe

手势处理程序

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

public boolean onTouch(final View v, final MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        result = onSwipeRight();
                    } else {
                        result = onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        result = onSwipeBottom();
                    } else {
                        result = onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public boolean onSwipeRight() {
    return false;
}

public boolean onSwipeLeft() {
    return false;
}

public boolean onSwipeTop() {
    return false;
}

public boolean onSwipeBottom() {
    return false;
}

}

0 个答案:

没有答案