Android调整ListView的ScrollBar的顶部位置

时间:2013-07-17 19:29:23

标签: android listview android-listview scrollbar

我创建了一个控制器,将视图添加到ListView.addHeaderView(...)中,该视图将管理标题的一部分以粘贴到ListView的顶部。我这样做的方法是让控件监视OnScrollListener,当标题的一部分位于ListView的顶部,然后从标题中删除pinnable视图并将其添加到ListView的父视图的顶部。

这项工作正是我期望豁免ListView右侧的滚动条将在我已固定到ListView顶部的视图后面。我需要让条形向下偏移固定视图的高度。

有没有办法调整ScrollBar顶部的开始位置?

我感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

这似乎工作正常:它是ListView.computeVerticalScrollOffset的副本,有两处修改:

  • 子类ListView,修改您的布局以使用您的子类
  • ListView.computeVerticalScrollOffset复制到您的自定义ListView
  • 用getter替换成员变量引用(见下文)
  • 修改偏移方法以考虑您要使用的调整(见下文)

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;
}
相关问题