Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if view is scrolled out of the screen

I have layout with scrollView as 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 detect if the transparentLayout (LinearLayout) is scrolled out of the screen.

like image 654
Hulk Avatar asked Jun 19 '15 07:06

Hulk


1 Answers

I solved it in this fashion:

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

                }

            }
        });
like image 91
Hulk Avatar answered Sep 19 '22 16:09

Hulk