GridView的滚动偏移 [英] Scroll offset of GridView

查看:311
本文介绍了GridView的滚动偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to determine the current scroll offset or scroll position of a GridView?

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

我已经尝试设置一个OnScrollListener但onScroll回调不细够我的目的粒度。

I have tried setting an OnScrollListener but the onScroll callback is not fine grained enough for my purposes.

下面是如何我试图确定滚动使用OnScrollListener偏移。

Here is the how I'm attempting to determine the scroll offset using an OnScrollListener.

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

本code的问题是,返回的y偏移向上滚动时不准确;顶视图被再循环,因此,该y偏移似乎跳;

The issue with this code is that the returned y offset is inaccurate when scrolling upwards; the top view is recycled and hence, the y offset seems to jump;

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

Is there a nice way of calculating the scroll offset of a GridView? I can't seem to find a good solution.

推荐答案

使用此。

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

这篇关于GridView的滚动偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆