Search code examples
androidscrollviewlistenerhorizontalscrollview

Is it possible to trigger an event when an element of my scroll view reach a certain position?


I'm using an HorizontalScroll view with android.

I would like to know if technically, it is possible to trigger an event (like changing the text of a textview ) when a specific element of my scrollview reach a certain position (for example when an element become the first visible element starting from the left of the scrollview).

Thanks in advance.


Solution

  • You can do it in this way:

    Rect scrollViewBounds = new Rect();
    scrollView.getHitRect(scrollViewBounds);
    
    if (yourElementView.getLocalVisibleRect(scrollViewBounds)) {
        // your element (or portion of it) is in visible area
        // you can trigger your event
    } else {
        // your element is not in visible area
    }