Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect the position of the scroll nestedscrollview android at the bottom?

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 ?

like image 636
Amay Diam Avatar asked Mar 21 '16 23:03

Amay Diam


People also ask

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

What is the difference between ScrollView and NestedScrollView in Android?

NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView. But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed. So to bring back smooth scrolling there's trick: ViewCompat.

How do I stop NestedScrollView scrolling?

setnestedscrollingenabled set it to false.


2 Answers

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");            }        }     }); } 
like image 158
Webserveis Avatar answered Sep 22 '22 10:09

Webserveis


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..

like image 44
V-rund Puro-hit Avatar answered Sep 19 '22 10:09

V-rund Puro-hit