How to determine the scroll position of the nested scrollview of android below?

I just want to determine the scroll position of the nestedscrollview android below and the 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 is not the same as MAX ScrollY. How to fix it?

+22
android android-nestedscrollview
Mar 21 '16 at 23:51
source share
5 answers

Set setOnScrollChangeListener in the NestedScrollView parameters to get

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

To determine if the offset is at the bottom, you need to get the content height value v.getChildAt(0).getMeasuredHeight() and compare the current scroll by the height of the parent element, if you have the same value, it means that it has reached the end.

You can get the height using the 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"); } } }); } 
+76
Jun 04 '16 at 12:10
source share

I know this 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.

+12
Jun 03 '16 at 4:54 on
source share

Webserve answered correctly, but it needs to change a bit in onScrollChange (override method), for example 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 } 
+1
Apr 29 '19 at 4:17
source share
  @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"); } } } 

It worked for me, source.

0
Dec 06 '18 at 10:34
source share

for api <23 you can add treeObserver.scrollChangeLister to store a local floating-point variable and check how your scroll 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"); } } 
0
Jun 14 '19 at 21:12
source share



All Articles