fun calculateScrollY(firstVisiblePosition: Int, visibleItemCount: Int): Int { mFirstVisiblePosition = firstVisiblePosition if (mReferencePosition < 0) { mReferencePosition = mFirstVisiblePosition } if (visibleItemCount > 0) { val c: View = mListView.getListChildAt(0) var scrollY = -c.top mListViewItemHeights.put(firstVisiblePosition, c.measuredHeight) return if (mFirstVisiblePosition >= mReferencePosition) { for (i in mReferencePosition until firstVisiblePosition) { if (mListViewItemHeights.get(i) == null) { mListViewItemHeights.put(i, c.measuredHeight) } scrollY += mListViewItemHeights.get(i) } scrollY } else { for (i in mReferencePosition - 1 downTo firstVisiblePosition) { if (mListViewItemHeights.get(i) == null) { mListViewItemHeights.put(i, c.measuredHeight) } scrollY -= mListViewItemHeights.get(i) } scrollY } } return 0 }
Call from an AbsListView.OnScrollListener to calculate the scrollY ( Here we definite as the distance in pixels compared to the position representing the current date ) .