Is there any event fired when android View becomes visible within App?

不羁的心 提交于 2020-07-18 03:37:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!