尝试在所有api中向上滚动滚动视图

时间:2014-09-19 08:14:03

标签: android api scrollview compatibility

我正在使用下一个源代码向上滚动滚动视图,但我看到方法removeOnGlobalLayoutListener在API 16下不兼容。

public void scrollUp(){
        // Wait until my scrollView is ready
        sv_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ready, move up
                sv_container.fullScroll(View.FOCUS_UP);
                sv_container.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

您是否知道在所有API中实现此目的的方法?感谢

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

<强>解决:

public void scrollUp(){
        // Wait until my scrollView is ready
        sv_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ready, move up
                sv_container.fullScroll(View.FOCUS_UP);
                if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    sv_container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    sv_container.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    }