Search code examples
androidpositionscrollviewscreendetection

Position of views on the screen


How to detect if cardview located in scrollview(linearlayout) went beyond the screen Or returned back to the field of view? I tried to do this through OnScrollChanged But it seems to me that this is not the case, and it is impossible to get the correct algorithm of work


Solution

  • You have to use the method getLocalVisibleRect() on your CardView:

    Rect scrollViewRect = new Rect();
    // Get the visible Rect of the ScrollView.
    scrollView.getHitRect(scrollViewRect);
    // Check if CardView's Rect is inside it.
    if (!cardView.getLocalVisibleRect(scrollViewRect)) {
        // The CardView is not visibile anymore.
    }
    

    You can place this code wherever you want to check if the CardView is not visible anymore on the screen.