Detect if view scrolls from screen

I have a layout with scrollView as shown below:

<ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

              <LinearLayout
                    android:id="@+id/transparentLayout"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@android:color/transparent"
                    android:orientation="vertical" >
              </LinearLayout>

              ... other views

</ScrollView>

I want to determine if a transparent window ( LinearLayout ) scrolls off screen.

+4
source share
1 answer

I solved it this way:

scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

            @Override
            public void onScrollChanged() {

                Rect scrollBounds = new Rect();
                scrollView.getHitRect(scrollBounds);
                if (layout.getLocalVisibleRect(scrollBounds)) {
                    // if layout even a single pixel, is within the visible area do something

                } else {
                    // if layout goes out or scrolled out of the visible area do something

                }

            }
        });
+9
source

All Articles