• Android中监听ScrollView滑动停止和滑动到底部


    1.监听ScrollView滑动停止:

    /********************监听ScrollView滑动停止*****************************/
    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();
    				}
    			}
    		}
    	};
    
    
    	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;
    		scrollY = scroller.getScrollY();
    	}
    });
    /***********************************************************/



    2.监听ScrollView滑动到底部:

    package com.example.webviewdemo;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ScrollView;
    
    public class ScrollBottomScrollView extends ScrollView {
    
    	private ScrollBottomListener scrollBottomListener;
    
    	public ScrollBottomScrollView(Context context) {
    		super(context);
    	}
    
    	public ScrollBottomScrollView(Context context, AttributeSet attrs) {
    		super(context, attrs);
    	}
    
    	public ScrollBottomScrollView(Context context, AttributeSet attrs,int defStyle) {
    		super(context, attrs, defStyle);
    	}
    
    	@Override
    	protected void onScrollChanged(int l, int t, int oldl, int oldt){
    		if(t + getHeight() >=  computeVerticalScrollRange()){
    			//ScrollView滑动到底部了
    			scrollBottomListener.scrollBottom();
    		}
    	}
    
    	public void setScrollBottomListener(ScrollBottomListener scrollBottomListener){
    		this.scrollBottomListener = scrollBottomListener;
    	}
    
    	public interface ScrollBottomListener{
    		public void scrollBottom();
    	}
    
    }

    重写ScrollView的onScrollChanged的方法。

  • 相关阅读:
    SpringMvc框架总结
    Spring框架总结
    Redis常用数据类型
    从配置文件中获取list,set,map值
    Oracle数据库编码格式不同造成乱码
    事务
    Spring-动态代理
    关于Maven项目pom.xml文件不报错却有红叉的问题
    Spring—SSJ集成&声明式事务管理
    Spring-构造注入&注解注入&代理模式&AOP
  • 原文地址:https://www.cnblogs.com/roccheung/p/5797440.html
Copyright © 2020-2023  润新知