Reputation: 2591
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 ?
Upvotes: 48
Views: 76561
Reputation: 192
I just changed a little codes of first answer to detect reaching end of recyclerview inside nested scrollview.
binding.nestedScroll.setOnScrollChangeListener(object:NestedScrollView.OnScrollChangeListener {
override fun onScrollChange(
v: NestedScrollView,
scrollX: Int,
scrollY: Int,
oldScrollX: Int,
oldScrollY: Int
) {
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 (v.getChildAt(0).getBottom() <= v.height + scrollY)
{
Log.i("TAG", "BOTTOM SCROLL IN Recyclerview.");
Toast.makeText(requireContext(), "Last", Toast.LENGTH_LONG).show()
}
}
})
Upvotes: 1
Reputation: 1213
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 ");
}
}
});
Upvotes: 0
Reputation: 753
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.
Upvotes: 5
Reputation: 188
Webserveis answered right, but the condition should be like this
if (scrollY == (v?.getChildAt(0)?.measuredHeight ?: 0) - (v?.measuredHeight ?: 0)) {
//at bottom
}
Upvotes: 0
Reputation: 2419
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");
}
}
Upvotes: 0
Reputation: 139
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
}
Upvotes: 10
Reputation: 2873
Set setOnScrollChangeListener
in a NestedScrollView
params to get
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");
}
}
});
}
Upvotes: 121
Reputation: 73
@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
Upvotes: 6
Reputation: 5534
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..
Upvotes: 46