ScrollView布局实际上是一个垂直滚动条。
为什么 ScrollView 方法需要 x 参数,而它只能垂直滚动?
难道不应该只接受 y 参数吗?因为 x 参数没有用。
有什么想法吗?
最佳答案
代码来自ScrollView.class
。
ScrollView
需要调用View
的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
只能垂直方向滚动呢?
查看 ScrollView
中的 public boolean onTouchEvent(MotionEvent ev)
方法。
它只处理 ev.getY()
!
另一方面,HorizonalScrollView
仅处理 ev.getX()
。
关于android - ScrollView ScrollBy 方法有 2 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19221016/