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

时间:2015-08-12 11:15:38

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

当我在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)。如果我滑动较少的数量,文本将按预期更改为“滑动更多”但如果我滑过900然后在日志中我得到

  

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中跟踪它并更改按钮的颜色。像显示进步的东西。

1 个答案:

答案 0 :(得分: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;
    }
});
相关问题