问题
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
回答1:
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:
- I don't know your code and it is not posted. The code
viewTemp = convertView
is just an example, code from using an Adapter. - This is a listener when the layout is drawn for whatever reason which is many.
来源:https://stackoverflow.com/questions/31995545/is-there-any-event-fired-when-android-view-becomes-visible-within-app