2

ScrollView 布局实际上是一个垂直滚动条。

为什么滚动视图方法需要 x 参数,而它只垂直滚动?

它不应该只需要 y 参数吗?因为 x 参数是无用的。

有任何想法吗?

4

2 回答 2

2

代码来自ScrollView.class.
ScrollView需要调用View's scrollTo(x, y),所以它需要 2 个参数。
因此,任何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);
        }
    }
}

那为什么ScrollView只在垂直方向滚动呢?
看中的public boolean onTouchEvent(MotionEvent ev)方法ScrollView
它只处理ev.getY()
另一方面,HorizonalScrollView只有句柄ev.getX()

于 2013-10-07T10:02:33.790 回答
1

为什么滚动视图方法需要 x 参数,而它只垂直滚动?

仅当您的布局文件与视图的宽度相同时才会出现这种情况。看看这个线程。

您也可以使用 水平滚动HorizontalScrollView,您将需要 x 参数。检查开发人员网站

或者,如果需要,您可以使用此博客中显示的二维滚动条来使用这两个参数。

于 2013-10-07T09:53:10.663 回答