【问题标题】:Issue with Left to Right Swipe Gesture in AndroidAndroid中从左到右滑动手势的问题
【发布时间】:2015-08-12 11:15:38
【问题描述】:

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

我有一个附有 onTouchListener 的按钮 (300*20dp)。从左到右滑动它会触发一个事件并向我的服务器发送一个 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)。如果我滑动较少,文本会按预期更改为“滑动更多”,但如果我滑动超过 900 次,那么在日志中我会得到 ​​p>

08-12 16:09:20.521 25598-25598/com.brandlabs.varun.yocity D/Motion 是:执行了从左到右的滑动

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

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

2) 为什么会这样?

3) 使用此功能时,当用户移动手指时,我可以在 action_move 期间跟踪它并更改按钮的颜色吗?比如显示进度。

【问题讨论】:

    标签: android performance android-layout swipe android-progressbar


    【解决方案1】:

    为什么会这样?

    发生这种情况是因为当您滑动屏幕时,一旦您达到阈值 900,您将不会在恰好相差 900 处抬起手指。很明显,您的手指会走得更远,例如,差异901、902、……并且每一个都将满足该 if 条件并在您的 logcat 中生成另一个日志。

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

    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;
        }
    });
    

    【讨论】:

    • 是的,我在发布问题几小时后添加了一个计数器。即使没有
    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2011-07-23
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多