37

i just want to detect the position of the scroll nestedscrollview android at the bottom, and the to call function. my code is :

scroll.getViewTreeObserver()
      .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
           @Override
           public void onScrollChanged() {
               int totalHeight = scroll.getChildAt(0).getHeight();
               int scrollY = scroll.getScrollY();
               Log.v("position", "totalHeight=" + totalHeight + "scrollY=" + scrollY);
               if (scrollY==totalHeight) {
                   getPlaylistFromServer("more");
               }
           }
      });

but totalheight not same wit MAX ScrollY. how to fix it ?

ColdFire
  • 6,116
  • 6
  • 32
  • 50
Amay Diam
  • 2,299
  • 6
  • 30
  • 53

8 Answers8

106

Set setOnScrollChangeListener in a NestedScrollView params to get

  • NestedScrollView v (parent with scroll)
  • int scrollY
  • int oldScrollY

To detect whether the offset is at the bottom, it is necessary to obtain the value of content height v.getChildAt(0).getMeasuredHeight() and compare the current scroll over the height of the parent, if you have the same value , it means that it has reached the end.

You can get the height with parent view with v.getMeasuredHeight()

NestedScrollView scroller = (NestedScrollView) findViewById(R.id.myScroll);

if (scroller != null) {

    scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            if (scrollY > oldScrollY) {
                Log.i(TAG, "Scroll DOWN");
            }
            if (scrollY < oldScrollY) {
                Log.i(TAG, "Scroll UP");
            }

            if (scrollY == 0) {
                Log.i(TAG, "TOP SCROLL");
            }

           if (scrollY == ( v.getMeasuredHeight() - v.getChildAt(0).getMeasuredHeight() )) {
               Log.i(TAG, "BOTTOM SCROLL");
           }
       }
    });
}
Webserveis
  • 1,924
  • 1
  • 18
  • 22
  • 2
    How to check when the user stopped scrolling like once it becomes idle? – Alaa AbuZarifa Aug 24 '17 at 06:24
  • 1
    Would someone be able to explain why this line works? `if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()))` How is it that the `NestedScrollView`'s measured height is smaller than the content's height, even though the `NestedScrollView` is the parent? – coolDude Mar 09 '18 at 22:59
  • much better v.getMeasuredHeight() - v.getChildAt(0).getMeasuredHeight(, I change now – Webserveis Mar 10 '18 at 14:27
  • 12
    min API for this approach is 23, do you know another alternative? – João Carlos Aug 07 '18 at 17:46
  • 4
    @JoãoCarlos no this is not from 23 API. `setOnScrollChangeListener(View.OnScrollChangeListener)` needs API 23, but `setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener)` doesn't – grine4ka Apr 30 '20 at 09:56
  • 1
    You are the hero of my day, bro! Your answer helped me a lot. Thanks! – Mark Delphi Oct 02 '20 at 19:44
34

I know it's late but.. try this way.

scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
            View view = (View) scroll.getChildAt(scroll.getChildCount() - 1);

            int diff = (view.getBottom() - (scroll.getHeight() + scroll
                    .getScrollY()));

            if (diff == 0) {
                getPlaylistFromServer("more");
            }          
    }
});

Happy Coding..

V-rund Puro-hit
  • 5,270
  • 8
  • 27
  • 48
7

Webserveis answered right, but it needs a little bit of changes in onScrollChange (override method), like this:

if (scrollY === v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
   // end of the scroll view
}

Kotlin:

if (scrollY == v.getChildAt(0).measuredHeight - v.measuredHeight) {
    // end of the scroll view
}
Denis Tsoi
  • 5,957
  • 5
  • 26
  • 46
Wai Yan
  • 109
  • 1
  • 4
4
  @Override
public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    if (nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1) != null) {
        if ((scrollY >= (nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1).getMeasuredHeight() - nestedScrollView.getMeasuredHeight())) &&
                scrollY > oldScrollY) {
            LogsUtils.INSTANCE.makeLogD(">onScrollChange>", ">>BOTTOm");
        }

    }
}

Its worked for me, Source

1

This worked for me :

nestedScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            if (v.getChildAt(0).getBottom()<=(nestedScroll.getHeight()+scrollY)) {
                System.out.println("End of NestedScrollView");
            }

        }
    });

Basically , we add many views inside a nestedScrollView but wrap them together in a form of Single View. Therefore, childCount will always be 1 with its index 0. Thus, used v.getChildAt(0).getBottom() to determine its bottom. Now, nestedScroll.getHeight() returns height in pixel,and , scrollY will return current vertical origin. Therefore, the above condition will be true everytime the bottom of NestedScrollView is reached.

if (v.getChildAt(0).getBottom()==(nestedScroll.getHeight()+nestedScroll.getScrollY()))

This only works some time... therefore, don't use it in such way.

Mrudul Tora
  • 390
  • 2
  • 11
0

for api <23 you can add a treeObserver.scrollChangeLister store a local float variable and check which way your scrolling like this

example

public class About extends AppCompatActivity implements 
ViewTreeObserver.OnScrollChangedListener{

private float viewScrolled = 0;

   nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(this);

}

@Override
public void onScrollChanged() {

    if (viewScrolled < nestedScrollView.getScrollY()){
        viewScrolled = nestedScrollView.getScrollY();
        Log.d(TAG, "scrolling up");
    }
    if (viewScrolled > nestedScrollView.getScrollY()){
        viewScrolled = nestedScrollView.getScrollY();
        Log.d(TAG, "scrolling down");
    }
}
martinseal1987
  • 747
  • 2
  • 25
  • 52
0

Webserveis answered right, but the condition should be like this

 if (scrollY == (v?.getChildAt(0)?.measuredHeight ?: 0) - (v?.measuredHeight ?: 0)) {
    //at bottom
    }
0

This worked for me!

            my_scroll_view.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {



                    if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
                        Log.d(TAG, "onScrollChange: SRCOLLED ==> scrollY: ======================================> THIS IS THE VERY END ");
                    }

                

            }
        });
MUHINDO
  • 7
  • 3