您的位置:首页 > 其它

ScrollView判断是否滑动到底部和顶部

2016-10-08 17:15 211 查看
getMeasuredHeight()是实际View的大小,与屏幕无关,而getHeight的大小此时则是屏幕的大小。当超出屏幕后, getMeasuredHeight() 等于 getHeight()加上屏幕之外没有显示的大小

滚动到顶部判断:

getScrollY() == 0

滚动到底部判断:

View childView = getChildAt(0);

childView.getMeasuredHeight() <= getScrollY() + getHeight();



其中getChildAt表示得到ScrollView的child View

childView.getMeasuredHeight()表示得到子View的高度,

 getScrollY()表示得到y轴的滚动距离,



getHeight()为scrollView可见的高度即屏幕的高度

getScr
b4f5
ollY()达到最大时加上scrollView的高度就的就等于它内容的高度了.

判断滑动位置的地方,可以有两种方式:

1、实现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;
}
}


2、重写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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: