My App contains multiple views (scrollable), one of them is CustomView ( extends View), is there any android event fired when this View comes within visible area.
The approach i though of using background thread during the timeframe of onAttachedToWindow
to onDetachedToWindow
This thread will use customView.getGlobalVisibleRect(rectangle)
to check whether any portion of the view is visible on the screen but this is spin lock approach, Is there any better way to detect visibility, any event which gets fired where I can call getGlobalVisibleRect
An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.
Events are a useful way to collect data about a user's interaction with interactive components of Applications. Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis.
Here is one sample code using ViewTreeObserver:
final View viewTemp = convertView;
convertView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int rowHeight = viewTemp.getGlobalVisibleRect();
...
});
Notes:
viewTemp = convertView
is just an example, code from using an Adapter.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