怎么判断android中ScrollView滑动到了最底部?

滚动到顶部判断:
getScrollY() == 0
滚动到底部判断:
View childView = getChildAt(0);
childView.getMeasuredHeight() <= getScrollY() + getHeight();
其中getChildAt表示得到ScrollView的child View
childView.getMeasuredHeight()表示得到子View的高度,
getScrollY()表示得到y轴的滚动距离,
getHeight()为scrollView的高度
getScrollY()达到最大时加上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);
}
}
本文欢迎转载,转载请注明出处与作者
出处:http://blog.sina.com.cn/staratsky
作者:流星

android开发群,分享开发中问题的解决办法和经验,欢迎大家的加入,群号:293872059
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值