滾動到頂部判斷:android
getScrollY() == 0ide
滾動到底部判斷:函數
View childView = getChildAt(0);spa
childView.getMeasuredHeight() <= getScrollY() + getHeight();orm
其中getChildAt表示獲得ScrollView的child Viewblog
childView.getMeasuredHeight()表示獲得子View的高度,開發
getScrollY()表示獲得y軸的滾動距離,get
getHeight()爲scrollView的高度it
getScrollY()達到最大時加上scrollView的高度就的就等於它內容的高度了.io
判斷滑動位置的地方,能夠有兩種方式:
一、實現OnTouchListener來監聽是否滑動到最底部
OnTouchListener onTouchListener=new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (childView != null && childView .getMeasuredHeight() <= getScrollY() + getHeight()) {
} else if (getScrollY() == 0) {
}
break;
}
return false;
}
}
二、重寫ScrollView的onScrollChanged的方法,在onScrollChanged函數中判斷
public class myScrollView extends ScrollView
{
public myScrollView(Context context)
{
super(context);
}
public myScrollView(Context context, AttributeSet attributeSet)
{
super(context,attributeSet);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
View view = (View)getChildAt(getChildCount()-1);
int d = view.getBottom();
d -= (getHeight()+getScrollY());
if(d==0)
{
//you are at the end of the list in scrollview
//do what you wanna do here
}
else
super.onScrollChanged(l,t,oldl,oldt);
}
}
本文歡迎轉載,轉載請註明出處與做者
出處:http://blog.sina.com.cn/staratsky
做者:流星
android開發羣,分享開發中問題的解決辦法和經驗,歡迎你們的加入,羣號:293872059