How to get the offset of a view inside a ScrollView?

后端 未结 1 801
囚心锁ツ
囚心锁ツ 2021-01-04 18:37

There is a ScrollView and which has many children views. Is there any way to get the offset-y to the top of the ScrollView for every child?

相关标签:
1条回答
  • 2021-01-04 19:13

    Hmmmm, not sure if this is the quickest, best way to do this, but if you get the view's location on screen, and you know the top of the ScrollView, you can calculate the offset of the view relative to the ScrollView. To get the position of a View in the screen:

    int[] location = {0,0};
    view.getLocationOnScreen(location);
    

    location[1] has the Y value. You will get negative values when the view is "above" the viewport.

    0 讨论(0)
提交回复
热议问题