懒加载的Scrollview

本文介绍了一个名为LazyScrollView的自定义ScrollView,该控件包含了滚动到底部、顶部和中间位置的监听功能。通过设置OnScrollListener接口,可以监听滚动事件并在相应位置触发回调方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要实现一个功能:当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. }  
package com.ql.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
 
public class LazyScrollView extends ScrollView{
	private static final String tag="LazyScrollView";
	private Handler handler;
	private View view;
	public LazyScrollView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	public LazyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	//这个获得总的高度
	public int computeVerticalScrollRange(){
		return super.computeHorizontalScrollRange();
	}
	public int computeVerticalScrollOffset(){
		return super.computeVerticalScrollOffset();
	}
	private void init(){
		
		this.setOnTouchListener(onTouchListener);
		handler=new Handler(){
        	@Override
			public void handleMessage(Message msg) {
				// process incoming messages here
				super.handleMessage(msg);
				switch(msg.what){
				case 1:
					if(view.getMeasuredHeight() <= getScrollY() + getHeight()) {
						if(onScrollListener!=null){
							onScrollListener.onBottom();
						}
						
					}else if(getScrollY()==0){
						if(onScrollListener!=null){
							onScrollListener.onTop();
						}
					}
					else{
						if(onScrollListener!=null){
							onScrollListener.onScroll();
						}
					}
					break;
				default:
					break;
				}
			}
        };
		
	}
	
	  OnTouchListener onTouchListener=new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					break;
				case MotionEvent.ACTION_UP:
					if(view!=null&&onScrollListener!=null){
						handler.sendMessageDelayed(handler.obtainMessage(1), 200);
					}
					break;

				default:
					break;
				}
				return false;
			}
	    	
	    };
	    
	    /**
	     * 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。
	     */
	    public void getView(){
	    	this.view=getChildAt(0);
	    	if(view!=null){
	    		init();
	    	}
	    }
	    
	    /**
	     * 定义接口
	     * @author admin
	     *
	     */
	    public interface OnScrollListener{
	    	void onBottom();
	    	void onTop();
	    	void onScroll();
	    }
	    private OnScrollListener onScrollListener;
	    public void setOnScrollListener(OnScrollListener onScrollListener){
	    	this.onScrollListener=onScrollListener;
	    }
}


用的时候也很简单,通常这样使用:
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.         });  
scrollView=(LazyScrollView)findViewById(R.id.scrollView);
        scrollView.getView();
        scrollView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onTop() {
				// TODO Auto-generated method stub
				Log.d(tag,"------滚动到最上方------");
			}
			
			@Override
			public void onScroll() {
				// TODO Auto-generated method stub
				Log.d(tag,"没有到最下方,也不是最上方");
			}
			
			@Override
			public void onBottom() {
				// TODO Auto-generated method stub
				Log.d(tag,"------滚动到最下方------");
			}
		});

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值