Android 如何获取 ScrollView 的当前 Y 偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3327087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:44:05  来源:igfitidea点击:

How to get the current Y offset of a ScrollView

android

提问by Rich

ScrollView has a method for setting the x and y scroll offset, but no method for getting the current offset (all I'm really interested is the y offset, since ScrollView only supports vertical scrolling). I don't see any method that would work in the superclasses, and tried getTop() for the content view, which is always zero. Am I missing something?

ScrollView 有设置 x 和 y 滚动偏移的方法,但没有获取当前偏移的方法(我真正感兴趣的是 y 偏移,因为 ScrollView 只支持垂直滚动)。我没有看到任何可以在超类中工作的方法,并尝试了内容视图的 getTop(),它始终为零。我错过了什么吗?

回答by Charles Harley

Call getScrollY()on the ScrollView

调用getScrollY()ScrollView

See here for the documentation: http://developer.android.com/reference/android/view/View.html#getScrollY%28%29

有关文档,请参见此处:http: //developer.android.com/reference/android/view/View.html#getScrollY%28%29

回答by Jun

If you are certain that you should get some value after using getScrollY() or getTop(), try to put those method inside a

如果您确定在使用 getScrollY() 或 getTop() 后应该获得一些值,请尝试将这些方法放入

yourScroolView.post(new Runnable() {
@Override
public void run() {
    Toast.makeText(getApplicationContext(),"Current Y is : "+getScrollY,Toast.LENGTH_SHORT).show();
            }
        });

Now it should work. According to my understanding about this method, it will only run after the layout being drawn. That can be one of the reason why you kept getting 0 previously. Hope it helps.

现在它应该可以工作了。根据我对这种方法的理解,它只会在绘制布局后运行。这可能是您之前一直获得 0 的原因之一。希望能帮助到你。

回答by Nikola Velimirovic

Why don't you try something like this ?

你为什么不尝试这样的事情?

targetScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            int scrollX = targetScrollView.getScrollX();
            Log.d(TAG, "scrollX: " + scrollX);
        }
    });

回答by pleonasmik

I achieve to get by the following. First get screen heigh and width.

我通过以下方式实现。首先获取屏幕高度和宽度。

DisplayMetrics metrics = getResources().getDisplayMetrics();
float screenWidth = metrics.widthPixels;
float screenHeight = metrics.heightPixels;

then to find out the document height (the total, what is being shown and what is out of the screen)

然后找出文档高度(总数,显示的内容以及屏幕外的内容)

public float getDocumentHeight(){
    return (computeVerticalScrollRange() * screenHeight)/computeVerticalScrollExtent();
}

Finally to get the offset

最后得到偏移量

public float getTopY(){
    return (getDocumentHeight() * computeVerticalScrollOffset())/computeVerticalScrollRange();
}

This give you the top of the window relative to the part of the document you are seeing so you could find the exact position of an event by

这为您提供了相对于您正在查看的文档部分的窗口顶部,以便您可以通过以下方式找到事件的确切位置

public boolean onTouchEvent(MotionEvent event) {
      int y = event.getY() + (int) getTopY();
    }

You can also do something similar to handle the width

你也可以做一些类似的事情来处理宽度