在android中的水平滚动视图中确定哪些视图项可见且最中心?

时间:2014-05-20 16:27:53

标签: android android-layout android-listview android-linearlayout android-scrollview

我在xml中定义了自定义水平滚动视图:

<com.myapp.views.MyHorizontalScrollView
    android:id="@+id/myScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/myLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

    </LinearLayout>
</com.myapp.views.MyHorizontalScrollView>

我正在动态膨胀并将子视图添加到线性布局(如上所述)。到目前为止,这很好用。

此外,我已经扩展了水平滚动视图。这是添加一个onscroll监听器,它给我onscroll事件,似乎正在工作

问题

当用户在滚动视图上滚动时,我需要确定用户现在是否可以看到任何视图,即屏幕上显示的视图。

另外,我想确定scrollview中最中心的视图(再次对用户可见)

这可能吗?

1 个答案:

答案 0 :(得分:0)

确定视图是否可见,您可以像Bill Mote那样执行此操作

 Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
    // Any portion of the imageView, even a single pixel, is within the visible window
} else {
    // NONE of the imageView is within the visible window
}

是对此问题Android: how to check if a View inside of ScrollView is visible?

的回答

在那里你可以看到哪一个更集中在一个小数学和你添加你的意见的顺序