GridView的滚动偏移量

时间:2013-07-03 16:14:45

标签: android gridview scroll scroll-offset

有没有办法确定GridView的当前滚动偏移或滚动位置?

View.getScrollY() // Underlying var is not updated during a scroll.

我已经尝试过设置一个OnScrollListener,但onScroll回调的细粒度不足以达到我的目的。

以下是我尝试使用OnScrollListener确定滚动偏移的方法。

private int getScrollY() {
    int top = 0;
    if (mGridView.getChildCount() > 0) {
        final View firstView = mGridView.getChildAt(0);
        top = firstView.getTop();
    }
    return top;
}

此代码的问题是向上滚动时返回的y偏移是不准确的;顶视图被回收,因此,y偏移似乎跳跃;

有没有一种很好的方法来计算GridView的滚动偏移量?我似乎无法找到一个好的解决方案。

2 个答案:

答案 0 :(得分:4)

使用此功能。

public class CustomGridView extends GridView {

    public CustomGridView(Context context) {
        super(context);
    }

    public CustomGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /* ADD THIS */
    @Override
    public int computeVerticalScrollOffset() {
        return super.computeVerticalScrollOffset();
    }
}

调用

时返回int

答案 1 :(得分:1)

相关问题