Android - ScrollView 监听滑动停止,顶部还是底部?

来源:互联网 发布:新网域名管理密码 编辑:程序博客网 时间:2024/06/05 22:50

//ScrollView嵌套Listview时候–监听滑动真正停止
//实习期间遇到的小问题,杜酿的时候未一下子找到正确解决; 整理如下

//判断滑动停止scrollview.setOnTouchListener(new OnTouchListener() {    private int lastY = 0;    private int touchEventId = -9983761;    Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            View scroller = (View)msg.obj;            if(msg.what==touchEventId) {                if(lastY ==scroller.getScrollY()) {                    handleStop(scroller);                }else {                    handler.sendMessageDelayed(handler.obtainMessage(touchEventId,scroller), 5);                    lastY = scroller.getScrollY();                }            }        }    };    @Override    public boolean onTouch(View v, MotionEvent event) {        if(event.getAction() == MotionEvent.ACTION_UP) {            handler.sendMessageDelayed(handler.obtainMessage(touchEventId,v), 5);        }        return false;    }    //这里写真正的滑动停止事件    private void handleStop(Object view) {        ScrollView scroller = (ScrollView) view;        System.out.println(scroller.getScrollY());        System.out.println(scroller.getHeight());        //判断滑动停止的位置时顶部还是底部?        if(childview.getMeasuredWidth() <= (scroller.getWidth() + scroller.getScrollX() + 5)){              /***                                                     *滑动到底部的操作                    */                                                             }          if(scroller.getScrollX() < 0){                        /***                                                     *滑动到顶部的操作             */                        }      }});

如何判断滑动到顶部还是底部详细请参照:http://blog.csdn.net/CL18652469346/article/details/53170210
以下只粗略介绍:

//判断滑动到顶部: 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);    }}
1 0
原创粉丝点击