I have a ScrollView and there is such a listener

@Override protected void onScrollChanged(int iX, int iY, int iOldX, int iOldY) { if (mOnScrollListener != null) { mOnScrollListener.onScrollChanged(this, iX, iY, iOldX, iOldY); if (iY >= iOldY) { mOnScrollListener.onGoDown(); } else { mOnScrollListener.onGoUp(); } } } 

But the problem is that as soon as I scroll down (for example), then the onGoDown() method is called 100 times and this is logical I agree

What condition to make this method be called only once if the user scrolls up and once if down

  • It is necessary to set the flag that this user has been processed and not to call your method anymore. But the problem is when to remove this flag. After all, the user can put a finger and scroll up and down, and can svapnut and release. In general, it depends on what kind of behavior you need. - eugeneek

1 answer 1

Most likely you need to keep two flags

 boolean isScrollDown, isScrollUp; if (iY >= iOldY) { if(isScrollDown) return; mOnScrollListener.onGoDown(); isScrollDown = true; isScrollUp = false; } else { if(isScrollUp) return; mOnScrollListener.onGoUp(); isScrollDown = false; isScrollUp = true; } 

This is vskidku