如何检测自定义滚动视图的结束?

时间:2015-03-02 05:06:54

标签: android scrollview

我已经完成了垂直滚动视图。我的要求是,我只需要检测滚动视图的结尾并使按钮可见。任何人都可以告诉我如何检测到滚动视图已经到达底部?

public class LockableVerScroll extends ScrollView {

private boolean isScrollable;
private ScrollListener scrollListener=null;
public LockableVerScroll(Context context)
{
    super(context);
    isScrollable=true;
}

public LockableVerScroll(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    isScrollable=true;
}
public LockableVerScroll(Context context, AttributeSet attrs,int defStyle)
{
    super(context, attrs, defStyle);
    isScrollable=true;
}
public boolean isScrollable()
{
    return isScrollable;
}
public boolean setScrollable(boolean mScrollable)
{
    return mScrollable=isScrollable;
}
public void setScrollViewListener(ScrollListener mscrollListener)
{
    this.scrollListener=mscrollListener;

}

需要帮助..提前谢谢。!!

2 个答案:

答案 0 :(得分:2)

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = (View) getChildAt(getChildCount()-1);
        int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));// Calculate the scrolldiff
        if( diff == 0 ){  // if diff is zero, then the bottom has been reached
            Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
        }
        super.onScrollChanged(l, t, oldl, oldt);
}

答案 1 :(得分:1)

覆盖onScrollChanged()

扩展中的ScrollView方法
@Override
protected void onScrollChanged(int l1, int t1, int l2, int t2) {

    int count = getChildCount();
    View view = (View) getChildAt(count - 1);
    int delta = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));
    if( delta == 0 )
    {
        // scrollview has reached bottom, do whatever you want here.
    }
    super.onScrollChanged(l1, t1, l2, t1);
}

试试这个。这将有效。

修改

if( delta == 0 )
{
    // define interface in Activity / Fragment and call its method here
    mScrollListener.onScrollViewHitsBottom();
}