I have a custom horizontal scrollview defined as in 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>
I am dynamically inflating and adding child view to the linear layout (as above). This works nicely so far.
Also, I have extended the Horizontal scrollview. This is to add an onscroll listener which gives me onscroll event, and seems to be working
Question
When the user scrolls across on the scrollview, I need to determine if any of the views are now visible to the user i.e. shown on screen.
Also, I would like to determine the most centered view in scrollview (again that is visible to the user)
is this possible?
to determine if a View is visible you can do this like 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 }
is stated as answer to this question Android: how to check if a View inside of ScrollView is visible?
there you can play to see which one is more centered with a little math and the order in which you added your views