differenciate Swipe和tap getures android

时间:2015-10-09 17:03:14

标签: android ontouchlistener gesturedetector

我试图制作笔记应用并遇到一些问题: 我在自定义LinearLayout中有自定义Edittext并尝试在Edittext上处理滑动。我正在使用这种滑动识别解决方案OnSwipeTouchListener并且它工作得很完美,直到我点击edittext(在女巫案例中,edittext没有获得焦点)。我将OnSwipeTouchListener设置为edittext.ontouchlistener     公共类OnSwipeTouchListener实现View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);

}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

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


    @Override
    public boolean onSingleTapConfirmed(MotionEvent e){
        print(211212, "singleTap shemovida");
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        print(21512512, "fling");
        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) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

static void print(int line, String st){
    PRINT.print("ONSwipeTouchListener", line, st);
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

我将其设置为edittext

    void addTextView(){
    _myText = new CustomEditText(getContext());
    LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    _myText.setLayoutParams(params);
    _myText.setId(getEditTextId());
    addView(_myText);
    _myText.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
        @Override
        public void onSwipeDown() {

        }

        @Override
        public void onSwipeLeft() {
            CustomEditTextWithCheckbox.this.onSwipeLeft();
        }

        @Override
        public void onSwipeUp() {
        }

        @Override
        public void onSwipeRight() {
            CustomEditTextWithCheckbox.this.onSwipeRight();
        }
    });
}

但我猜OnSwipeTouchListener.OnTouch总是返回true,因此edittext无法获得焦点。 我也尝试改变onTouch

 public boolean onTouch(final View view, final MotionEvent motionEvent) {
   gestureDetector.onTouchEvent(motionEvent);
    return false;
}

并且它可以工作,但仍然存在问题是,当我想要滑动时,刷卡edittext会获得焦点,而且我理解为什么它会像那样工作。但我需要的是,如果用户不刷卡而不是触摸应该返回false,否则为true。 任何人都可以帮忙

1 个答案:

答案 0 :(得分:1)

我为我的问题here找到了一个很好的解决方案 你需要用这种方式改变一点SwipeDismissTouchListener

{{1}}

以这种方式运作完美,希望它会有所帮助