简体   繁体   English

ScrollView ScrollBy方法采用2个参数

[英]ScrollView ScrollBy method takes 2 parameters

ScrollView layout is actually a vertical scroller. ScrollView布局实际上是一个垂直滚动器。

Why the scroll view method takes x parameter while it only scrolls vertically? 为什么滚动视图方法仅在垂直滚动时采用x参数?

Shouldn't it take only the y parameter? 它不应该只使用y参数吗? since the x parameter is useless. 因为x参数没有用。

Any ideas? 有任何想法吗?

Code comes from ScrollView.class . 代码来自ScrollView.class
ScrollView need to call View 's scrollTo(x, y) , so it takes 2 parameters. ScrollView需要调用ViewscrollTo(x, y) ,因此它需要2个参数。
Therefore, any View 's subclass, including ScrollView , can scroll by x axis and y axis. 因此,任何View的子类,包括ScrollView ,都可以按x轴和y轴滚动。

@Override
public void scrollTo(int x, int y) {
    // we rely on the fact the View.scrollBy calls scrollTo.
    if (getChildCount() > 0) {
        View child = getChildAt(0);
        x = clamp(x, getWidth() - mPaddingRight - mPaddingLeft, child.getWidth());
        y = clamp(y, getHeight() - mPaddingBottom - mPaddingTop, child.getHeight());
        if (x != mScrollX || y != mScrollY) {
            super.scrollTo(x, y);
        }
    }
}

Then why ScrollView only scrolls in vertical direction? 那么,为什么ScrollView仅沿垂直方向滚动?
Look at public boolean onTouchEvent(MotionEvent ev) method in ScrollView . ScrollView查看public boolean onTouchEvent(MotionEvent ev)方法。
It only handles ev.getY() ! 它只处理ev.getY()
On the other hand, HorizonalScrollView only handles ev.getX() . 另一方面, HorizonalScrollView仅处理ev.getX()

Why the scroll view method takes x parameter while it only scrolls vertically? 为什么滚动视图方法仅在垂直滚动时采用x参数?

This is only the case if your layout file is the same width as your view. 仅当布局文件的宽度与视图的宽度相同时才如此。 Take a look at this thread. 看一下这个线程。

You can also scroll horizontaly using HorizontalScrollView , there you will need the x parameter. 您还可以使用HorizontalScrollView进行HorizontalScrollView滚动,这里将需要x参数。 Check the developer site for this. 检查开发者网站

Or you can use this two dimensional scroller shown in this blog to make use of the both of the parameters if needed. 或者,如果需要,可以使用本博客中显示的二维滚动条来使用这两个参数。

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

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