Android:ScrollView的总高度

时间:2010-08-31 13:05:54

标签: android scroll scrollview

我有一个自定义的ScrollView(扩展的android.widget.ScrollView),我在我的布局中使用它。我想测量此scrollview内容的总高度。 getHeight()和getMeasuredHeight()不会给我正确的值(数字太高)。

背景信息:我想确定用户滚动了多远。我使用onScrollChanged来获取X值,但我需要知道一个百分比,所以我需要总滚动条高度。

非常感谢! 埃里克

2 个答案:

答案 0 :(得分:91)

ScrollView总是有一个孩子。您需要做的就是获得孩子的身高以确定总身高:

int totalHeight = scrollView.getChildAt(0).getHeight();

答案 1 :(得分:2)

查看ScrollView的来源。不幸的是,这种方法是私有的,但您可以将其复制到您自己的代码中。 请注意,其他答案不考虑填充

private int getScrollRange() {
    int scrollRange = 0;
    if (getChildCount() > 0) {
        View child = getChildAt(0);
        scrollRange = Math.max(0,
                child.getHeight() - (getHeight() - mPaddingBottom - mPaddingTop));
    }
    return scrollRange;
}