Android 中从左向右滑动手势的问题

Issue with Left to Right Swipe Gesture in Android

当我在 LG G6 上接听电话时,我必须单击绿色圆圈的中心并向外滚动到外圈,该圆圈伴随着颜色变化才能接听电话。我想在我的应用中实现类似的功能。

我有一个按钮 (300*20dp),它附有一个 onTouchListener。从左向右滑动会触发事件并向我的服务器发送 POST 请求,否则不会发送任何请求。

我目前拥有的是

Button
    android:layout_marginTop="10dp"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="Swipe Right to Purchase!"
    android:id="@+id/btnSend"

我在MainActivity中的代码如下

Button btnSend;
btnSend=(Button)findViewById(R.id.btnSend);

btnSend.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                myGestDetector.onTouchEvent(event);
                //Log.d(" VIEW : ",v.getLeft()+"  "+v.getRight());                    
                return true;
            }
        });
 myGestDetector = new GestureDetector(this, new 

GestureDetector.SimpleOnGestureListener()
        {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {    
            if (e2.getX()- e1.getX()>900) {
                Log.d(TAG, "Left to Right swipe performed");
                btnSend.setText("Order has been placed!");

            }else btnSend.setText("Swipe More!");
            return true;
        }

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
            return true;
        }
    });

这确保用户必须水平滑动大量内容。现在我将 900 乘以 0.75*(view.getRight-view.getLeft)。如果我滑动较少的数量,文本会按预期更改为 "swipe more",但如果我滑动超过 900,那么在日志中我会得到

08-12 16:09:20.521 25598-25598/com.brandlabs.varun.yocity D/Motion is﹕ Left to Right swipe performed

单次滑动会出现多次,导致发送多个 POST 请求。我的问题是

1) 我怎样才能停止这个/将它限制为一个事件?如果我取消注释 Log.d(" VIEW : ",v.getLeft()+" "+v.getRight());然后每次滑动我都会在日志中得到多行这些行。

2) 为什么会这样?

3) 使用这个时,当用户移动他的手指时,我可以在 action_move 期间跟踪它并改变按钮的颜色。显示进度之类的东西。

Why is this happening?

发生这种情况是因为当您在达到阈值 900 时滑动屏幕时,您不会在正好差 900 时抬起手指。很明显,您的手指会移动得更远,例如,差异 901, 902, ...每一个都会满足 if-condition 并在您的 logcat.

中产生另一个日志

要解决此问题,请将您的 GestureDetector class 更改为

myGestDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    boolean swipePerformed = true;

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if(!swipePerformed){    
            if (e2.getX()- e1.getX()>900) {
                Log.d(TAG, "Left to Right swipe performed");
                btnSend.setText("Order has been placed!");
                swipePerformed = true;
            } else btnSend.setText("Swipe More!");
            return true;
        }
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e1)
    {
        swipePerformed = false;
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        return false;
    }
});