检测视图是否滚出屏幕

时间:2015-06-19 07:22:56

标签: android scrollview

我的scrollView布局如下:

<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>

我想检测transparentLayout( LinearLayout )是否滚出屏幕。

1 个答案:

答案 0 :(得分:11)

我以这种方式解决了它:

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

                }

            }
        });