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
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…