W
W
wolfak2016-01-19 16:13:49
Java
wolfak, 2016-01-19 16:13:49

Event when ScrollView is scrolled to the end?

Good evening.
I am developing an application for android and faced the need to reload content when the scroll reaches the end of the page.
How can this be implemented? Maybe there is some event on the ScrollView?
Searched the Internet, found nothing useful. Preferably with a detailed example.
Tried this method, doesn't work:

@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); 
}

Maybe I'm just not using it correctly. We need a detailed usage example from the scroll declaration itself.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kornachev, 2016-01-19
@zelan

In response to comments on the question:
Wolfak : The "Override" annotation says that this method is overridden. It can be redefined ONLY IN THAT class where it has already been described earlier.
Such a method is in the ScrollView class, respectively, it is in it that this method must be overridden and an object created.
This method is called by the scrollview whenever the position of the scrollview changes. Accordingly, you need to make a change in the logic of work (add your own actions).
There are 2 common ways (there are others).
First:

ScrollView scroll = new ScrollView(context){     //context - то что должно быть внутри скрола
     @Override 
      protected void onScrollChanged(int l, int t, int oldl, int oldt)  
      { 
             //тут, надо вставить то, что хочешь ( то что описано в твоем методе
      }
}

Insert the resulting object in the right place.
Second:
Inherit from ScrollView and insert your method (just insert the method you provided). Create an instance of the inherited class and use.
If you do everything correctly, then in the log window, when you scroll down to the very bottom, the message "MyScrollView: Bottom has been reached" will fly out.
Frankly, the method that I took will not always work.
In general, learn the basics of java and INHERITANCE, so that such questions would not arise (about super and overriding methods did not arise.). These are the basics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question