ScrollView减缓滑动事件

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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容