簡體   English   中英

如何檢測scrollview的孩子到達屏幕頂部?

[英]how to detect child of scrollview reached top of screen?

我有很多視圖的ScrollView,如何知道滾動后特定視圖是否到達屏幕頂部?

我試過了

mScroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

            @Override
            public void onScrollChanged() {

                Rect scrollBounds = new Rect();
                mScroll.getHitRect(scrollBounds);

                if (mView.getLocalVisibleRect(scrollBounds) ) {

                    // if layout even a single pixel, is within the visible area do something
                    //Do someThing
                } else {

                    // if layout goes out or scrolled out of the visible area do something
                    // Do some thing
                }

            }
        });

但是,此代碼檢測視圖是否在屏幕之外而不是在屏幕上方

在與所需位置進行比較之后,使用View.getLocationOnScreen()和/或getLocationInWindow()

您可以創建一個類以自定義ScrollView。 在此類中,重寫方法onScrollChanged(int l,int t,int oldl,int oldt);

這是我的垂直ScrollView代碼。

private int rangeEndHeight = 20;
private boolean onEnd = false;

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    int length = (int) Math.floor(getContentHeight() * getScale());
    if(getScrollY() >= length - getHeight() - rangeEndHeight) {
        if (!onEnd) {
            onEnd = true;

            // callback here

        }
    }
    else {
        onEnd = false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM