GestureDetector onScroll() issue (executed 3 times)

前端 未结 1 922
北海茫月
北海茫月 2021-01-15 08:08

I have this snippet of code to detect a scroll gesture using GestureDetector. It works, except it detects the scroll activity 3 times instead of once.

How can I mak

相关标签:
1条回答
  • 2021-01-15 08:51

    "onScroll()" will be called multiple times. How many times it will be called would depend on the scroll action done by the user.

    If you want your code block to run only once at the beginning of each scroll action then you would have to add a condition, something as below:

       float scrollstartX1, scrollStartY1;
    
       public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
               float distanceY) {
        // get x and Y co-ordinates and log it as info.
           if (scrollstartX1 != e1.getX() || scrollStartY1 != e1.getY()) {
               scrollstartX1 = e1.getX();
               scrollStartY1 = e1.getY();
                   //***************************************
               //code run only once for a scroll action...
                   //****************************************
           }
               float x2 = e2.getX();
               float y2 = e2.getY();
               Log.i("Scroll_Gesture", "Scrolled from: (" + scrollstartX1 + "," + scrollStartY1 + " to "
                       + x2 + "," + y2 + ")");
    
               mp = MediaPlayer.create(this, R.raw.scroll_success);
               mp.start();
    
               // start success page
               Intent intent = new Intent(this, ScrollSuccess.class);
               startActivity(intent);
    
           return false;
       }
    
    0 讨论(0)
提交回复
热议问题