這似乎工作確定:這是ListView.computeVerticalScrollOffset的副本兩個改變:
- 子類
ListView
,修改您的佈局中使用你的子類
- 複製
ListView.computeVerticalScrollOffset
到您的自定義ListView
- 更換成員變量引用與獲取者(見下文)
- 修改偏移方法以說明您想要使用的調整(請參閱下文)
YourListView.java
:
@Override
protected int computeVerticalScrollOffset() {
final int firstPosition = getFirstVisiblePosition();
final int childCount = getChildCount();
if (firstPosition >= 0 && childCount > 0) {
if (isSmoothScrollbarEnabled()) {
final View view = getChildAt(0);
final int top = view.getTop();
int height = view.getHeight();
if (height > 0) {
// The core of the change is here (mHeaderRowHeight)
return Math.max(firstPosition * 100 - (top * 100)/height +
(int) ((float) (getScrollY() + mHeaderRowHeight)/(getHeight() + mHeaderRowHeight) * getCount() * 100), 0);
}
} else {
int index;
final int count = getCount();
if (firstPosition == 0) {
index = 0;
} else if (firstPosition + childCount == count) {
index = count;
} else {
index = firstPosition + childCount/2;
}
return (int) (firstPosition + childCount * (index/(float) count));
}
}
return 0;
}