1

I've been trying to work out how to calculate the y-position of scrolled content inside an Android ListView. The consensus seems to be that Android simply doesn't give you this information and you have to work it out by doing all sorts of calculations based on the getChildAt(0).top and getFirstVisiblePosition() methods.

My question is to try to understand why Android doesn't give you this information. It would seem to that the the list view must actually know this value somehow in order to draw the scrollbars. Or at least know it as a percentage? The ViewPager's onPageScrolled() method seems to do the same thing. And it's a trivial matter in iOS as well.

Mick Byrne
  • 14,394
  • 16
  • 76
  • 91
  • 1
    `getScrollY()` is not doing what you are trying to do? – Lawrence Choy Nov 26 '13 at 05:26
  • 1
    getScrollY() always returns 0 on a ListView, as noted on other SO questions, such as http://stackoverflow.com/questions/12727594/android-listview-current-scroll-location-y-pixels Why is it always 0, I'd love to know, seems like a very misleading method to me. – Mick Byrne Nov 26 '13 at 05:28

1 Answers1

4

ListView is not same as ScrollView as you thought of.

For example, if you have 10 items with use 2048px hight. If you wrap them in scroll view, the inside content is 2048px height. However, in ListView, the 10 items will be populated dynamically. It is always in the displaying area, thus the scrollY = 0.

If you really want to calculate the ScrollY of ListView, you might need to take care of it by yourself. You can getFirstVisiblePosition() to get the index of the current top visible item. And consider the height of each list item and list divider, you might be able to get the total scrollY value.

Robin
  • 10,052
  • 6
  • 31
  • 52
  • You've explained why `getScrollY()` equals 0, at least in terms of flaky Android semantics, but this still doesn't answer my main question. So ups for you for contributing, but no tick. – Mick Byrne Nov 26 '13 at 05:46
  • I have updated my answer. You the getScrollY() is giving the information other than you want. The scroll distance you need can be calculated by your self. – Robin Nov 26 '13 at 05:47