懒加载的Scrollview


from : http://gundumw100.iteye.com/blog/1164066


要实现一个功能:当Scrollview滑动到最底端的时候需要触发事件加载其他数据。很多人都以为ScrollView可以像ListViev那样setOnScrollListener,其实沒那么简单,因为ScrollView压根就没有该接口,在baidu上兜了一圈没有找到合适的答案,没办法只能google去了,居然一下子解决了这个问题,还是老外比较牛,呵呵,这是我访问的网址: 
http://stackoverflow.com/questions/2864563/how-do-i-know-that-the-scrollview-is-already-scrolled-to-the-bottom 

注意,如果数据不满一页的话,会执行onBottom方法!通常要使用懒加载的话数据都会超过一页,所以我沒仔细考虑这个问题! 

我把ScrollView封装成类了,源码如下: 

Java代码   收藏代码
  1. package com.ql.view;  
  2.   
  3. import android.content.Context;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.util.AttributeSet;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.widget.ScrollView;  
  10.    
  11. public class LazyScrollView extends ScrollView{  
  12.     private static final String tag="LazyScrollView";  
  13.     private Handler handler;  
  14.     private View view;  
  15.     public LazyScrollView(Context context) {  
  16.         super(context);  
  17.         // TODO Auto-generated constructor stub  
  18.     }  
  19.     public LazyScrollView(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         // TODO Auto-generated constructor stub  
  22.     }  
  23.     public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {  
  24.         super(context, attrs, defStyle);  
  25.         // TODO Auto-generated constructor stub  
  26.     }  
  27.     //这个获得总的高度  
  28.     public int computeVerticalScrollRange(){  
  29.         return super.computeHorizontalScrollRange();  
  30.     }  
  31.     public int computeVerticalScrollOffset(){  
  32.         return super.computeVerticalScrollOffset();  
  33.     }  
  34.     private void init(){  
  35.           
  36.         this.setOnTouchListener(onTouchListener);  
  37.         handler=new Handler(){  
  38.             @Override  
  39.             public void handleMessage(Message msg) {  
  40.                 // process incoming messages here  
  41.                 super.handleMessage(msg);  
  42.                 switch(msg.what){  
  43.                 case 1:  
  44.                     if(view.getMeasuredHeight() <= getScrollY() + getHeight()) {  
  45.                         if(onScrollListener!=null){  
  46.                             onScrollListener.onBottom();  
  47.                         }  
  48.                           
  49.                     }else if(getScrollY()==0){  
  50.                         if(onScrollListener!=null){  
  51.                             onScrollListener.onTop();  
  52.                         }  
  53.                     }  
  54.                     else{  
  55.                         if(onScrollListener!=null){  
  56.                             onScrollListener.onScroll();  
  57.                         }  
  58.                     }  
  59.                     break;  
  60.                 default:  
  61.                     break;  
  62.                 }  
  63.             }  
  64.         };  
  65.           
  66.     }  
  67.       
  68.       OnTouchListener onTouchListener=new OnTouchListener(){  
  69.   
  70.             @Override  
  71.             public boolean onTouch(View v, MotionEvent event) {  
  72.                 // TODO Auto-generated method stub  
  73.                 switch (event.getAction()) {  
  74.                 case MotionEvent.ACTION_DOWN:  
  75.                     break;  
  76.                 case MotionEvent.ACTION_UP:  
  77.                     if(view!=null&&onScrollListener!=null){  
  78.                         handler.sendMessageDelayed(handler.obtainMessage(1), 200);  
  79.                     }  
  80.                     break;  
  81.   
  82.                 default:  
  83.                     break;  
  84.                 }  
  85.                 return false;  
  86.             }  
  87.               
  88.         };  
  89.           
  90.         /** 
  91.          * 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。 
  92.          */  
  93.         public void getView(){  
  94.             this.view=getChildAt(0);  
  95.             if(view!=null){  
  96.                 init();  
  97.             }  
  98.         }  
  99.           
  100.         /** 
  101.          * 定义接口 
  102.          * @author admin 
  103.          * 
  104.          */  
  105.         public interface OnScrollListener{  
  106.             void onBottom();  
  107.             void onTop();  
  108.             void onScroll();  
  109.         }  
  110.         private OnScrollListener onScrollListener;  
  111.         public void setOnScrollListener(OnScrollListener onScrollListener){  
  112.             this.onScrollListener=onScrollListener;  
  113.         }  
  114. }  


用的时候也很简单,通常这样使用: 
Java代码   收藏代码
  1. scrollView=(LazyScrollView)findViewById(R.id.scrollView);  
  2.         scrollView.getView();  
  3.         scrollView.setOnScrollListener(new OnScrollListener() {  
  4.               
  5.             @Override  
  6.             public void onTop() {  
  7.                 // TODO Auto-generated method stub  
  8.                 Log.d(tag,"------滚动到最上方------");  
  9.             }  
  10.               
  11.             @Override  
  12.             public void onScroll() {  
  13.                 // TODO Auto-generated method stub  
  14.                 Log.d(tag,"没有到最下方,也不是最上方");  
  15.             }  
  16.               
  17.             @Override  
  18.             public void onBottom() {  
  19.                 // TODO Auto-generated method stub  
  20.                 Log.d(tag,"------滚动到最下方------");  
  21.             }  
  22.         });  

感激我吧,我呕心沥血才出来了这么个类。呵呵。 

顺便记一下老外使用fullScroll的做法。当然也可以直接fullScroll而不需要放入post()。 
Java代码   收藏代码
  1. scrollView.post(new Runnable() {  
  2.            @Override  
  3.            public void run() {  
  4.             scrollView.fullScroll(View.FOCUS_DOWN);  
  5.            }  
  6.        });  

只要把fullScroll改成scrollTo就可以做一个书签效果了: 
http://yangsongjing.iteye.com/blog/1855063  

Android-ObservableScrollView 
https://github.com/ksoichiro/Android-ObservableScrollView  

在HorizontalScrollView中使用ScrollView相互影响问题的解决办法:  
On my ScrollView, I needed to override the onInterceptTouchEvent method to only intercept the touch event if the Y motion is > the X motion. It seems like the default behavior of a ScrollView is to intercept the touch event whenever there is ANY Y motion. So with the fix, the ScrollView will only intercept the event if the user is deliberately scrolling in the Y direction and in that case pass off the ACTION_CANCEL to the children. 

Here is the code for my Scroll View class that contains the HorizontalScrollView: 
Java代码   收藏代码
  1. public class CustomScrollView extends ScrollView {  
  2.     private GestureDetector mGestureDetector;  
  3.     View.OnTouchListener mGestureListener;  
  4.   
  5.     public CustomScrollView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.         mGestureDetector = new GestureDetector(new YScrollDetector());  
  8.         setFadingEdgeLength(0);  
  9.     }  
  10.   
  11.     @Override  
  12.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  13.         return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);  
  14.     }  
  15.   
  16.     // Return false if we're scrolling in the x direction    
  17.     class YScrollDetector extends SimpleOnGestureListener {  
  18.         @Override  
  19.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  20.             if(Math.abs(distanceY) > Math.abs(distanceX)) {  
  21.                 return true;  
  22.             }  
  23.             return false;  
  24.         }  
  25.     }  
  26. }  


android监听ScrollView滑动停止  

在ScrollView中嵌入GridView  
http://fariytale.iteye.com/blog/1420254  
做android程序开发的都知道,不能在一个拥有Scrollbar的组件中嵌入另一个拥有Scrollbar的组件,因为这不科学,会混淆滑动事件,导致只显示一到两行数据。那么就换一种思路,首先让子控件的内容全部显示出来,禁用了它的滚动。如果超过了父控件的范围则显示父控件的scrollbar滚动显示内容,思路是这样,一下是代码。 
具体的方法是自定义GridView组件,继承自GridView。重载onMeasure方法: 
Java代码   收藏代码
  1. public class MyGridView extends GridView  
  2. {  
  3.     public MyGridView(android.content.Context context,  
  4.             android.util.AttributeSet attrs)  
  5.     {  
  6.         super(context, attrs);  
  7.     }  
  8.   
  9.     /** 
  10.      * 设置不滚动 
  11.      */  
  12.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  13.     {  
  14.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  15.                 MeasureSpec.AT_MOST);  
  16.         super.onMeasure(widthMeasureSpec, expandSpec);  
  17.   
  18.     }  
  19.   
  20. }  

其中onMeasure函数决定了组件显示的高度与宽度; 
makeMeasureSpec函数中第一个函数决定布局空间的大小,第二个参数是布局模式 
MeasureSpec.AT_MOST的意思就是子控件需要多大的控件就扩展到多大的空间 
之后在ScrollView中添加这个组件就OK了,同样的道理,ListView也适用。 

滚动监听的ScrollView  
Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.ScrollView;  
  4.   
  5. public class NotifyingScrollView extends ScrollView {  
  6.   
  7.     public interface OnScrollChangedListener {  
  8.         void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);  
  9.     }  
  10.   
  11.     private OnScrollChangedListener mOnScrollChangedListener;  
  12.   
  13.     public NotifyingScrollView(Context context) {  
  14.         super(context);  
  15.     }  
  16.   
  17.     public NotifyingScrollView(Context context, AttributeSet attrs) {  
  18.         super(context, attrs);  
  19.     }  
  20.   
  21.     public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.     }  
  24.   
  25.     @Override  
  26.     protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
  27.         super.onScrollChanged(l, t, oldl, oldt);  
  28.         if (mOnScrollChangedListener != null) {  
  29.             mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);  
  30.         }  
  31.     }  
  32.   
  33.     public void setOnScrollChangedListener(OnScrollChangedListener listener) {  
  34.         mOnScrollChangedListener = listener;  
  35.     }  
  36.   
  37. }  



ScrollView也可以实现OnTouchListener来监听是否滑动到最底部  
Java代码   收藏代码
  1. import android.os.Bundle;  
  2. import android.view.MotionEvent;  
  3. import android.view.View;  
  4. import android.view.View.OnTouchListener;  
  5. import android.widget.ScrollView;  
  6. import android.app.Activity;  
  7. /** 
  8.  * Demo描述: 
  9.  * 监听ScrollView滑动到顶端和底部 
  10.  *  
  11.  * 注意事项: 
  12.  * 1 mScrollView.getChildAt(0).getMeasuredHeight()表示: 
  13.  *   ScrollView所占的高度.即ScrollView内容的高度.常常有一 
  14.  *   部分内容要滑动后才可见,这部分的高度也包含在了 
  15.  *   mScrollView.getChildAt(0).getMeasuredHeight()中 
  16.  *    
  17.  * 2 view.getScrollY表示: 
  18.  *   ScrollView顶端已经滑出去的高度 
  19.  *    
  20.  * 3 view.getHeight()表示: 
  21.  *   ScrollView的可见高度 
  22.  *    
  23.  */  
  24. public class MainActivity extends Activity {  
  25.     private ScrollView mScrollView;  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         init();  
  31.     }  
  32.     private void init(){  
  33.         mScrollView=(ScrollView) findViewById(R.id.scrollView);  
  34.         mScrollView.setOnTouchListener(new TouchListenerImpl());  
  35.     }  
  36.     private class TouchListenerImpl implements OnTouchListener{  
  37.         @Override  
  38.         public boolean onTouch(View view, MotionEvent motionEvent) {  
  39.             switch (motionEvent.getAction()) {  
  40.             case MotionEvent.ACTION_DOWN:  
  41.    
  42.                 break;  
  43.             case MotionEvent.ACTION_MOVE:  
  44.                  int scrollY=view.getScrollY();  
  45.                  int height=view.getHeight();  
  46.                  int scrollViewMeasuredHeight=mScrollView.getChildAt(0).getMeasuredHeight();  
  47.                  if(scrollY==0){  
  48.                         System.out.println("滑动到了顶端 view.getScrollY()="+scrollY);  
  49.                     }  
  50.                  if((scrollY+height)==scrollViewMeasuredHeight){  
  51.                         System.out.println("滑动到了底部 scrollY="+scrollY);  
  52.                         System.out.println("滑动到了底部 height="+height);  
  53.                         System.out.println("滑动到了底部 scrollViewMeasuredHeight="+scrollViewMeasuredHeight);  
  54.                     }  
  55.                 break;  
  56.    
  57.             default:  
  58.                 break;  
  59.             }  
  60.             return false;  
  61.         }  
  62.            
  63.     };  
  64. }  


在滚动的视图观测滚动事件的Android库:Android-ObservableScrollView 
http://www.open-open.com/lib/view/open1415854429070.html  


Java代码   收藏代码
  1. import android.content.Context;    
  2. import android.util.AttributeSet;    
  3. import android.widget.ScrollView;    
  4.     
  5. public class ObservableScrollView extends ScrollView {    
  6.     
  7.     private ScrollViewListener scrollViewListener = null;    
  8.     
  9.     public ObservableScrollView(Context context) {    
  10.         super(context);    
  11.     }    
  12.     
  13.     public ObservableScrollView(Context context, AttributeSet attrs,    
  14.             int defStyle) {    
  15.         super(context, attrs, defStyle);    
  16.     }    
  17.     
  18.     public ObservableScrollView(Context context, AttributeSet attrs) {    
  19.         super(context, attrs);    
  20.     }    
  21.     
  22.     public void setScrollViewListener(ScrollViewListener scrollViewListener) {    
  23.         this.scrollViewListener = scrollViewListener;    
  24.     }    
  25.     
  26.     @Override    
  27.     protected void onScrollChanged(int x, int y, int oldx, int oldy) {    
  28.         super.onScrollChanged(x, y, oldx, oldy);    
  29.         if (scrollViewListener != null) {    
  30.             scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);    
  31.         }    
  32.     }    
  33.     
  34. }    


网上说的方法乱七八糟,能用的就是自己算高度,其实sdk-9中,ScrollView已经加入了一个方法,能监听到是否已经不能滚动,稍加处理,就可以监听是否滑到底部了。 

先上自定义的ScrollView方法: 
Java代码   收藏代码
  1. import android.content.Context;    
  2. import android.util.AttributeSet;    
  3. import android.widget.ScrollView;    
  4.     
  5. public class BottomScrollView extends ScrollView {    
  6.     
  7.     private OnScrollToBottomListener onScrollToBottom;    
  8.         
  9.     public BottomScrollView(Context context, AttributeSet attrs) {    
  10.         super(context, attrs);    
  11.     }    
  12.     
  13.     public BottomScrollView(Context context) {    
  14.         super(context);    
  15.     }    
  16.     
  17.     @Override    
  18.     protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,    
  19.             boolean clampedY) {    
  20.         super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);    
  21.         if(scrollY != 0 && null != onScrollToBottom){    
  22.             onScrollToBottom.onScrollBottomListener(clampedY);    
  23.         }    
  24.     }    
  25.         
  26.     public void setOnScrollToBottomLintener(OnScrollToBottomListener listener){    
  27.         onScrollToBottom = listener;    
  28.     }    
  29.     
  30.     public interface OnScrollToBottomListener{    
  31.         public void onScrollBottomListener(boolean isBottom);    
  32.     }    
  33. }    

调用方法: 
Java代码   收藏代码
  1. BottomScrollView scroll = (BottomScrollView)findViewById(R.id.id_scroll);    
  2.         scroll.setOnScrollToBottomLintener(new OnScrollToBottomListener() {    
  3.                 
  4.             @Override    
  5.             public void onScrollBottomListener(boolean isBottom) {    
  6.                 // TODO Auto-generated method stub    
  7.                 Log.e("SCROLLVIEW", isBottom + "");    
  8.     
  9.             }    
  10.         });    




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值