I have a View in my Activity. I want to detect when it has moved around the screen due to any actions (user scrolls, relative views resize shifting contents, etc.). This is meant to be in a library, so I can query the hierarchy, but I don't control it, nor can I modify it other than adding event listeners.
Is there any way to get this as an event, rather than polling?
There is an event for this: View.getViewTreeObserver().addOnScrollChangedListener()
final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener = ...;
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
getViewTreeObserver().addOnScrollChangedListener(mScrollChangedListener);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
getViewTreeObserver().removeOnScrollChangedListener(mScrollChangedListener);
}
Source: Android Source Code (4.0), android.view.SurfaceView.java:205
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With