1

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?

FlashAsh80
  • 1,357
  • 1
  • 16
  • 27

1 Answers1

0

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

Community
  • 1
  • 1
Pedro Teran
  • 1,200
  • 3
  • 17
  • 43
  • Thanks I tried that answer before (and it didn't seem to work), but now I have tried it again and it seems to do the trick. (Not sure what I was doing wrong before). What is the calculation I need to work out the most centred item? – FlashAsh80 May 20 '14 at 18:31
  • as long as the items on a Linear Layout come in order you should see the frist partially visible view and the last one and more less the view on the middle of both will be the centered one for example if you have 6 views from number 2 to 6 (2,3,4,5,6) the 4 is the middle one – Pedro Teran May 20 '14 at 19:18
  • Also if you have a odd number of views you can use scrollBounds.getWidth() - imageView.getWidth() to see wich of the views on the two sides are more visible to infere wich view is more centered from the two of the middle – Pedro Teran May 20 '14 at 19:25
  • getting the middle item of the visible items works well, but if I have extra padding on one of the child view say the first one, the middle of the visible item is longer at the centre. – FlashAsh80 May 21 '14 at 09:59
  • This is the correct answer for determining if items are visible on screen and getting the centered one (providing they are equal width). so marking it as the answer. – FlashAsh80 May 23 '14 at 11:20