Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 712
Pradeep Avatar asked Aug 13 '15 18:08

Pradeep


People also ask

How listener works in Android?

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.

What is an event in Android Studio?

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.


1 Answers

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.
like image 132
The Original Android Avatar answered Nov 10 '22 02:11

The Original Android