Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Detecting When ScrollView Hits Bottom [duplicate]

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.

like image 807
Peacecraft Avatar asked Feb 10 '11 05:02

Peacecraft


1 Answers

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

like image 90
Harry Joy Avatar answered Sep 17 '22 05:09

Harry Joy