Android: Detecting When ScrollView Hits Bottom [du

2019-01-13 03:35发布

问题:

Possible Duplicate:
How to trigger an event when scrollView reach the bottom with Android?

I have been trying for an hour or so to detect when my scrollView gets to the bottom of the screen. Due to the various screen sizes and what not involved with Android, I can't simply say it's at the bottom when its Y position reaches a certain value, but there's no method that detects when it is at the bottom that I found.

I'm sure there's a simple solution to this, subtracting the height of the view by some other variable or something, but for some reason it just isn't clicking for me. Any ideas would be appreciated, thank you.

回答1:

Try this:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) 
{
    // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
    View view = (View) getChildAt(getChildCount()-1);

    // Calculate the scrolldiff
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    // if diff is zero, then the bottom has been reached
    if( diff == 0 )
    {
        // notify that we have reached the bottom
        Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
    }

    super.onScrollChanged(l, t, oldl, oldt);
}

To implement this, extend ScrollView and then override the onScrollChanged method (inherited from View).

Reference: Android: Understanding when ScrollView has reached the bottom