简体   繁体   中英

detect ScrollView vertical scroll direction

I have tried using ObservableScrollView and CustomScrollView

The problem I'm getting is OnScrollChanged is not getting fired in any case. I want to detect if scrollview scrolls down or up. Any help is appreciated.

I tried like this also but no result

 mScrollView?.viewTreeObserver?.addOnScrollChangedListener {
            if (viewScrolled < mScrollView?.getScrollY()!!) {
                viewScrolled = mScrollView?.getScrollY()!!
                Log.d("scroll", "scrolling up")
            }
            if (viewScrolled > mScrollView?.getScrollY()!!) {
                viewScrolled = mScrollView?.getScrollY()!!
                Log.d("scroll", "scrolling down")
            }
        }

This listener works but the thing is how to detect scroll up and down in this case

 mScrollView?.viewTreeObserver?.addOnScrollChangedListener {}

Every instance of view in Kotlin holds reference to viewTreeObserver so when you scroll an list inside scrollview you should called an addScrollChangedListener() method.

and determine the scrollview based on this. It's Java code but you can convert to Kotlin.

scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        int scrollY = rootScrollView.getScrollY(); // For ScrollView
        int scrollX = rootScrollView.getScrollX(); // For HorizontalScrollView
        // DO SOMETHING WITH THE SCROLL COORDINATES
    }
});

My recommendation here is to utilize a GestureDetector and SimpleGestureListener on your scroll view. This will let you add the GestureDetector via setOnTouchListener . Just remember to return false from the listener you add, or you might get some unexpected behavior from the scroller (you don't want to consume touch events, just monitor them)

Here's the signature of the method in the SimpleGestureListener that you're interested in. This gives you the distance moved of Y, and it will be either positive or negative indicating down or up.

onScroll
Added in API level 1

public boolean onScroll (MotionEvent e1, 
                MotionEvent e2, 
                float distanceX, 
                float distanceY)

Here's the Android Training Page for Gesture Detection: https://developer.android.com/training/gestures/detector

More information and a note about onDown : GestureDetector.SimpleOnGestureListener and GestureDetectorCompat don't work. What's wrong with my code?

UPDATE:

Code should look something similar to this:

val scrollView = ScrollView(context)
val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
    override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
        // Use distanceY
        return false
    }
}
val gestureDetector = GestureDetector(context, gestureListener)
scrollView.setOnTouchListener { v, event ->
    gestureDetector.onTouchEvent(event)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM