关于ScrollView.scrollTo 不起作用

在实际开发中通常会遇到scrollview中嵌套warp的listview,不过某写情况会出现页面加载完后不是在顶部的问题,这时候我们就需要用到scrollTo的方法来做修正一般我们会直接使用

ScrollView.scrollTo(x,y)

经过调试,查看源码的scrollTo方法发现问题所在


    /**
     * {@inheritDoc}
     *
     * <p>This version also clamps the scrolling to the bounds of our child.
     */
    @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);
            }
        }
    }

再判断x和y的时候,只有满足条件者才会调用父类的scrollTo 方法,

正确纠正方法应该为

sv_scrollview.post {
                    sv_scrollview.scrollTo(0, 0)
}

猜你喜欢

转载自blog.csdn.net/fl2502923/article/details/109984022