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

滚动到顶部判断: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

相关文章
相关标签/搜索