2015-03-02 51 views
0

我已經完成了垂直滾動視圖。我的要求是什麼,我只需要檢測滾動視圖的結束並使按鈕可見。是否有人告訴我如何檢測滾動視圖到底?如何檢測自定義滾動視圖的結束?

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; 

} 

需要幫助..提前感謝。

回答

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

我需要把這個方法在上面的類或? – 2015-03-02 05:22:51

+0

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

+0

你瞭解代碼嗎? – 2015-03-02 05:24:39

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