简体   繁体   English

检测ScrollView垂直滚动方向

[英]detect ScrollView vertical scroll direction

I have tried using ObservableScrollView and CustomScrollView 我尝试使用ObservableScrollViewCustomScrollView

The problem I'm getting is OnScrollChanged is not getting fired in any case. 我遇到的问题是在任何情况下都不会触发OnScrollChanged。 I want to detect if scrollview scrolls down or up. 我想检测scrollview是向下滚动还是向上滚动。 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. Kotlin中的每个view实例都拥有对viewTreeObserver的引用,因此当您在scrollview中滚动列表时,应调用addScrollChangedListener()方法。

and determine the scrollview based on this. 并基于此确定滚动视图。 It's Java code but you can convert to Kotlin. 它是Java代码,但是您可以转换为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. 我的建议是在滚动视图上使用GestureDetectorSimpleGestureListener This will let you add the GestureDetector via setOnTouchListener . 这将使您可以通过setOnTouchListener添加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) 只要记住要从添加的侦听器中返回false,否则您可能会从滚动条中得到一些意外的行为(您不想消耗触摸事件,只需监视它们即可)

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. 这是您感兴趣的SimpleGestureListener中方法的签名。这使您可以移动Y的距离,它可以是正或负,表示向下或向上。

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 这是用于手势检测的Android培训页面: https : //developer.android.com/training/gestures/detector

More information and a note about onDown : GestureDetector.SimpleOnGestureListener and GestureDetectorCompat don't work. 有关onDown更多信息和注意onDownGestureDetector.SimpleOnGestureListener和GestureDetectorCompat不起作用。 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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