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

标签 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 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 条件并在您的 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;
    }
});

关于android - Android 中从左向右滑动手势的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963615/

相关文章:

android - 如何始终将物体放在相机前

android - 处理选项卡小部件时出现 java.lang.UnsupportedOperationException

java - 删除 TextView 内容时禁用按钮

Ruby each vs while 循环性能

sql - 当在很多地方使用 GETDATE() 时,使用变量是不是更好?

android - RelativeLayout 中其他两个 View 之间的中心 View

android - 单击按钮更改 fragment View

java - 如何使用 HttpClient 在 Android 中管理 PHP session ?

java - System.nanoTime 用于计算执行时间和性能影响

android - 如何在drawable android中添加内阴影效果?