Android ScrollView取消惯性滚动的方法

ScrollView中惯性滚动的效果,想让这个ScrollView慢一点滑动或者接近drag(拖拽)操作,就提出了添加阻尼的说法。只要重新fling方法即可,将velocity值极至缩小。

实例如下:

public class CustomHorizontalScrollView extends HorizontalScrollView {
  private Context context;
  private ScrollViewListenner listenner;
  private CustomHorizontalScrollView currentView;

  public CustomHorizontalScrollView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.context = context;
  }

  public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    this.context = context;
  }

  public CustomHorizontalScrollView(Context context, AttributeSet attrs,
                   int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
    this.context = context;
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    currentView = this;
    return super.onTouchEvent(ev);
  }

  @Override
  protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    // TODO Auto-generated method stub
    if (null != listenner) {
      this.listenner.onScrollChanged(currentView, l, t, oldl, oldt);
    }
    super.onScrollChanged(l, t, oldl, oldt);
  }

  public interface ScrollViewListenner {
    public void onScrollChanged(CustomHorizontalScrollView view, int l,
                  int t, int oldl, int oldt);
  }

  public void setScrollViewListenner(ScrollViewListenner listenner) {
    this.listenner = listenner;
  }

  /**
  *
  *阻尼:1000为将惯性滚动速度缩小1000倍,近似drag操作。
  @Override
  public void fling(int velocity) {
    super.fling(velocity / 1000);
  }
  */
}

以上这篇Android ScrollView取消惯性滚动的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。