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

来源:互联网 发布:霍先生家的安之知全集 编辑:程序博客网 时间:2024/05/16 12:13

1.监听ScrollView滑动停止:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /********************监听ScrollView滑动停止*****************************/  
  2. scrollView.setOnTouchListener(new OnTouchListener() {  
  3. private int lastY = 0;  
  4. private int touchEventId = -9983761;  
  5. Handler handler = new Handler() {  
  6. @Override  
  7. public void handleMessage(Message msg) {  
  8. super.handleMessage(msg);  
  9. View scroller = (View) msg.obj;  
  10. if (msg.what == touchEventId) {  
  11. if (lastY == scroller.getScrollY()) {  
  12. handleStop(scroller);  
  13. else {  
  14. handler.sendMessageDelayed(handler.obtainMessage(touchEventId, scroller), 5);  
  15. lastY = scroller.getScrollY();  
  16. }  
  17. }  
  18. }  
  19. };  
  20.   
  21.   
  22. public boolean onTouch(View v, MotionEvent event) {  
  23. if (event.getAction() == MotionEvent.ACTION_UP) {  
  24. handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);  
  25. }  
  26. return false;  
  27. }  
  28.   
  29.   
  30. private void handleStop(Object view) {  
  31. ScrollView scroller = (ScrollView) view;  
  32. scrollY = scroller.getScrollY();  
  33. }  
  34. });  
  35. /***********************************************************/  

2.监听ScrollView滑动到底部:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class ScrollBottomScrollView extends ScrollView {  
  2.   
  3.   
  4. private ScrollBottomListener scrollBottomListener;  
  5.   
  6. public ScrollBottomScrollView(Context context) {  
  7. super(context);  
  8. }  
  9.   
  10. public ScrollBottomScrollView(Context context, AttributeSet attrs) {  
  11. super(context, attrs);  
  12. }  
  13.   
  14. public ScrollBottomScrollView(Context context, AttributeSet attrs,int defStyle) {  
  15. super(context, attrs, defStyle);  
  16. }  
  17.   
  18. @Override  
  19. protected void onScrollChanged(int l, int t, int oldl, int oldt){  
  20. if(t + getHeight() >=  computeVerticalScrollRange()){  
  21. //ScrollView滑动到底部了  
  22. scrollBottomListener.scrollBottom();  
  23. }  
  24. }  
  25.   
  26. public void setScrollBottomListener(ScrollBottomListener scrollBottomListener){  
  27. this.scrollBottomListener = scrollBottomListener;  
  28. }  
  29.   
  30. public interface ScrollBottomListener{  
  31. public void scrollBottom();  
  32. }  
  33.   
  34. }  

重写ScrollView的onScrollChanged的方法。

0 0