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

问题描述:

我已经完成了垂直滚动视图。我的要求是什么,我只需要检测滚动视图的结束并使按钮可见。是否有人告诉我如何检测滚动视图到底?如何检测自定义滚动视图的结束?

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; 

} 

需要帮助..提前感谢。

重写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(); 
} 
+0

我需要把这个方法在上面的类或? – 2015-03-02 05:22:51

+0

是把它加到'LockableVerScroll'类 – 2015-03-02 05:24:00

+0

你了解代码吗? – 2015-03-02 05:24:39

@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); 
}