美文网首页
ScrollView减缓滑动事件

ScrollView减缓滑动事件

作者: heyzhuyue | 来源:发表于2017-06-29 10:49 被阅读53次
public class WZScrollView extends ScrollView {
    private static final String TAG = "WZScrollView";

    public WZScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Log.d(TAG, "WZScrollView: ");
    }

    public WZScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WZScrollView(Context context) {
        this(context, null);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        if (scrollCallBack != null)
            scrollCallBack.scrollChangedCallback(l, t, oldl, oldt);
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    public void scrollBy(int x, int y) {
        int z = 2;
        super.scrollBy(x, y / z);
    }

    OnScrollChangedCallback scrollCallBack;

    public void setScrollCallBack(OnScrollChangedCallback scrollCallBack) {
        this.scrollCallBack = scrollCallBack;
    }

    public interface OnScrollChangedCallback {
        void scrollChangedCallback(int l, int t, int oldl, int oldt);
    }

    /**
     * 滑轮处理
     */
    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
            switch (event.getAction()) {
            // process the scroll wheel movement...处理滚轮事件
            case MotionEvent.ACTION_SCROLL:
                // 获得垂直坐标上的滚动方向,也就是滚轮向下滚

                smoothScrollBy(0, ((int) event.getAxisValue(MotionEvent.AXIS_VSCROLL) * -30));

                return true;
            }
        }
        return super.onGenericMotionEvent(event);
    }
}

相关文章

网友评论

      本文标题:ScrollView减缓滑动事件

      本文链接:https://www.haomeiwen.com/subject/phlmcxtx.html