Can I have onScrollListener for a ScrollView?

android-scrollview detect scroll up and down
android-scrollview scroll end listener
android-scrollview detect scroll direction
onscrollchanged scrollview android
how to get scrollview position in android
recyclerview onscrolllistener
scrollview fillviewport
android webview detect scroll bottom

I am using a HorizontalScrollView in a layout and I need to identify the user have reached the start and end point of the scroll.

For ListView I have tried a the onScrollListener and it is possible to find the start and end point of scroll.

I tried to do the same in my Scrollview but it seems not possible. Is there any other possible ways to achieve what I need.


Every instance of View calls getViewTreeObserver(). Now when holding an instance of ViewTreeObserver, you can add an OnScrollChangedListener() to it using the method addOnScrollChangedListener().

You can see more information about this class here.

It lets you be aware of every scrolling event - but without the coordinates. You can get them by using getScrollY() or getScrollX() from within the listener though.

scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        int scrollY = rootScrollView.getScrollY(); // For ScrollView
        int scrollX = rootScrollView.getScrollX(); // For HorizontalScrollView
        // DO SOMETHING WITH THE SCROLL COORDINATES
    }
});

android - Can I have onScrollListener for a ScrollView?, I am using a HorizontalScrollView in a layout and I need to identify the user have reached the start and end point of the scroll. For ListView I have tried a the  Every instance of View calls getViewTreeObserver (). Now when holding an instance of ViewTreeObserver, you can add an OnScrollChangedListener () to it using the method addOnScrollChangedListener (). You can see more information about this class here. It lets you be aware of every scrolling event – but without the coordinates.


This might be very useful. Use NestedScrollView instead of ScrollView. Support Library 23.1 introduced an OnScrollChangeListener to NestedScrollView. So you can do something like this.

 myScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            Log.d("ScrollView","scrollX_"+scrollX+"_scrollY_"+scrollY+"_oldScrollX_"+oldScrollX+"_oldScrollY_"+oldScrollY);
            //Do something
        }
    });

Can I have onScrollListener for a ScrollView?, For ListView I have tried a the onScrollListener and it is possible to find the start and end point of scroll. I tried to do the same in my Scrollview  Every instance of View calls getViewTreeObserver(). Now when holding an instance of ViewTreeObserver, you can add an OnScrollChangedListener() to it using the method addOnScrollChangedListener(). You can see more information about this class here. It lets you be aware of every scrolling event - but without the coordinates.


Here's a derived HorizontalScrollView I wrote to handle notifications about scrolling and scroll ending. It properly handles when a user has stopped actively scrolling and when it fully decelerates after a user lets go:

public class ObservableHorizontalScrollView extends HorizontalScrollView {
    public interface OnScrollListener {
        public void onScrollChanged(ObservableHorizontalScrollView scrollView, int x, int y, int oldX, int oldY);
        public void onEndScroll(ObservableHorizontalScrollView scrollView);
    }

    private boolean mIsScrolling;
    private boolean mIsTouching;
    private Runnable mScrollingRunnable;
    private OnScrollListener mOnScrollListener;

    public ObservableHorizontalScrollView(Context context) {
        this(context, null, 0);
    }

    public ObservableHorizontalScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ObservableHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();

        if (action == MotionEvent.ACTION_MOVE) {
            mIsTouching = true;
            mIsScrolling = true;
        } else if (action == MotionEvent.ACTION_UP) {
            if (mIsTouching && !mIsScrolling) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onEndScroll(this);
                }
            }

            mIsTouching = false;
        }

        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldX, int oldY) {
        super.onScrollChanged(x, y, oldX, oldY);

        if (Math.abs(oldX - x) > 0) {
            if (mScrollingRunnable != null) {
                removeCallbacks(mScrollingRunnable);
            }

            mScrollingRunnable = new Runnable() {
                public void run() {
                    if (mIsScrolling && !mIsTouching) {
                        if (mOnScrollListener != null) {
                            mOnScrollListener.onEndScroll(ObservableHorizontalScrollView.this);
                        }
                    }

                    mIsScrolling = false;
                    mScrollingRunnable = null;
                }
            };

            postDelayed(mScrollingRunnable, 200);
        }

        if (mOnScrollListener != null) {
            mOnScrollListener.onScrollChanged(this, x, y, oldX, oldY);
        }
    }

    public OnScrollListener getOnScrollListener() {
        return mOnScrollListener;
    }

    public void setOnScrollListener(OnScrollListener mOnEndScrollListener) {
        this.mOnScrollListener = mOnEndScrollListener;
    }

}

View.OnScrollChangeListener, Note: Some views handle scrolling independently from View and may have their OnScrollListener to listen for changes in list scroll position. I have an array of arrays listed with horizontal scrollview. Right now, it's one array for each page and some arrays have small items so screen seems a bit empty. I want to list my items according to


If you want to know the scroll position of a view, then you can use the following extension function on View class:

fun View?.onScroll(callback: (x: Int, y: Int) -> Unit) {
    var oldX = 0
    var oldY = 0
    this?.viewTreeObserver?.addOnScrollChangedListener {
        if (oldX != scrollX || oldY != scrollY) {
            callback(scrollX, scrollY)
            oldX = scrollX
            oldY = scrollY
        }
    }
}

Can I have onScrollListener for a ScrollView?, For ListView I have tried a the onScrollListener and it is possible to find the start and end point of scroll. I tried to do the same in my Scrollview but it seems not  There are some great answers here, but my code can detect when scrolling stops without having to extend ScrollView class. every view instance can call getViewTreeObserver(). when Holding this instance of ViewTreeObserver you can add a OnScrollChangedListener using the function addOnScrollChangedListener().


You can use NestedScrollView instead of ScrollView. However, when using a Kotlin Lambda, it won't know you want NestedScrollView's setOnScrollChangeListener instead of the one at View (which is API level 23). You can fix this by specifying the first parameter as a NestedScrollView.

nestedScrollView.setOnScrollChangeListener { _: NestedScrollView, scrollX: Int, scrollY: Int, _: Int, _: Int ->
    Log.d("ScrollView", "Scrolled to $scrollX, $scrollY")
}

Listening to scroll events on Android views, For compatibility reasons, OnScrollChangeListenerCompat interface will have the signature of View.OnScrollChangeListener that we are trying  This can be reproduced anytime and it’s code independent (I’ve tried more than 20 scenarios). I don’t have much experience in Android yet, but looks like the scroll listener stops the onClick listener or something like that. Any help with this would be much appreciated. Thank you in advance. PD.


Listening to Scroll Events on Android Views, For compatibility reasons, OnScrollChangeListenerCompat interface will have the signature of View.OnScrollChangeListener that we are trying  I required having a listview inside a scrollview in order to prevent the entire screen from being extremely long. This allows you have to set the number of items displayed on the list, while the rest will be scrolled. I use the following class in my app and so far it seems to work terrific.


ScrollView, Keep in mind that ScrollViews must have a bounded height in order to work, since When true, the ScrollView will try to lock to only vertical or horizontal scrolling onScroll. Fires at most once per frame during scrolling. The frequency of the  I have an EndlessRecyclerView at the end of a NestedScrollView.EndlessRecyclerView means: when user scrolls to the bottom of the recyclerView it loads more data. This is already implemented and working elsewhere but when I put the recyclerView inside the NestedScrollView the OnScrollListener events doesn't fire.


android.widget.ScrollView.onScrollChanged java code examples , Context context;new ScrollView(context); LogFragment logFragment;new ScrollView(logFragment.getActivity onScrollChanged(l, t, oldl, oldt); if (​onScrollListener != null) onScrollListener. This may contain path information depending. I have a TableLayout inside a ScrollView, because of some special needs, I need to do some things when the ScrollView has reached the top. Is there such a listener to listen in on user's scrolling operations? Or, if there is no a ready listener, can I subclass the ScrollView (which will be painful I supposed) to achieve that?