Scrollview与Scroller用法及问题点小结

1.Scrollview 与Scroller的简介:

 ScrollView是可以实现控件在超出屏幕范围的情况下滚动显示的控件,其内部的滑动是基于Scroller来实现的。

Scroller类是为了实现View平滑滚动的一个Helper类。通常在自定义的View时使用,在View中定义一个私有成员mScroller = new Scroller(context)。设置mScroller滚动的位置时,并不会导致View的滚动,通常是用mScroller记录/计算View滚动的位置,再重写View的computeScroll(),完成实际的滚动。 

Scroller的相关API如下:

mScroller.getCurrX() //获取mScroller当前水平滚动的位置  
mScroller.getCurrY() //获取mScroller当前竖直滚动的位置  
mScroller.getFinalX() //获取mScroller最终停止的水平位置  
mScroller.getFinalY() //获取mScroller最终停止的竖直位置  
mScroller.setFinalX(int newX) //设置mScroller最终停留的水平位置,没有动画效果,直接跳到目标位置  
mScroller.setFinalY(int newY) //设置mScroller最终停留的竖直位置,没有动画效果,直接跳到目标位置  
  
//滚动,startX, startY为开始滚动的位置,dx,dy为滚动的偏移量, duration为完成滚动的自定义时间  
mScroller.startScroll(int startX, int startY, int dx, int dy) //使用默认完成时间250ms  
mScroller.startScroll(int startX, int startY, int dx, int dy, int duration)  
  
mScroller.computeScrollOffset() //返回值为boolean,true说明滚动尚未完成,false说明滚动已经完成。这是一个很重要的方法,通常放在View.computeScroll()中,用来判断是否滚动是否结束。 
列子:

public class SlowScrollView extends ScrollView {
	private final String TAG="SlowScrollView ";
    private Scroller mScroller;  
      
    public SlowScrollView(Context context) {  
        super(context);  
        mScroller = new Scroller(context);  
    }  
  
    public SlowScrollView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        mScroller = new Scroller(context);  
    }  
  
    public SlowScrollView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        mScroller = new Scroller(context);  
    }  
      
    //调用此方法滚动到目标位置  duration滚动时间  
    public void smoothScrollToSlow(int fx, int fy, int duration) {    
        int dx = fx - getScrollX();//mScroller.getFinalX();  普通view使用这种方法  
        int dy = fy - getScrollY();  //mScroller.getFinalY();    
        smoothScrollBySlow(dx, dy, duration);    
    }    
    
    //调用此方法设置滚动的相对偏移    
    public void smoothScrollBySlow(int dx, int dy,int duration) { 
    	Log.d(TAG, "smoothScrollBySlow(int dx, int dy,int duration)");
  
        //设置mScroller的滚动偏移量    
        mScroller.startScroll(getScrollX(), getScrollY(), dx, dy,duration);//scrollView使用的方法(因为可以触摸拖动)    
//        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, duration);  //普通view使用的方法  
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果    
    }   
      
    @Override    
    public void computeScroll() {    
    	Log.d(TAG, "s-computeScroll()");
        //先判断mScroller滚动是否完成    
        if (mScroller.computeScrollOffset()) {    
        	
            //这里调用View的scrollTo()完成实际的滚动    
        	smoothScrollTo(mScroller.getCurrX(), mScroller.getCurrY());
        	Log.d(TAG, "s-computeScroll()-滚动未完成- mScroller.getCurrY()="+ mScroller.getCurrY()); 
            //必须调用该方法,否则不一定能看到滚动效果    
            postInvalidate();    
        }    
        super.computeScroll();    
    }  
}

2.Scrollview的易错点:

对于smoothScrollBy()的用法,在实际使用时发现,当每次让Scrollview移动固定px时,log打印的信息看,每次移动的距离会有错误差,所以当连续相对移动时就会容易出席移动错位的现象,对此问题,可以使用smoothScrollTo()方法,如果是ViewGroup容器类,每次移动的位置可以使用子view的getTop或者getBottom()来实现单行的移动,这样验证后效果很好,例子如下:

mScrollView.smoothScrollToSlow(0, item.getBottom(), 700);





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值