安卓滑动与点击事件冲突

2019-03-22  本文已影响0人  liuweilyy

安卓实现左滑删除的时候滑动与子view的点击事件冲突。解决方案,在父视图内部进行拦截。调用onInterceptTouchEvent方法,返回false继续传递,返回true拦截;

public class SolveClickTouchConflictLayout extends RelativeLayout {


    public SolveClickTouchConflictLayout(Context context) {
        super(context);
    }

    public SolveClickTouchConflictLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SolveClickTouchConflictLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private boolean mScrolling;
    private float touchDownX;
    private float touchDownY;
    private final int distance = 100;
    float x2 = 0;
    float y2 = 0;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:

                touchDownX = ev.getX();
                touchDownY = ev.getY();
                mScrolling = false;

                break;
            case MotionEvent.ACTION_MOVE:
                x2 = ev.getX();
                y2 = ev.getY();

                if (Math.abs(touchDownX - ev.getX()) >= ViewConfiguration.get(getContext()).getScaledTouchSlop()||Math.abs(touchDownY - ev.getY()) >= ViewConfiguration.get(getContext()).getScaledTouchSlop()) {

                    if ((x2 - touchDownX > 0 && (Math.abs(x2 - touchDownX) > distance) || (x2 - touchDownX < 0 && (Math.abs(x2 - touchDownX) > distance)))){
                        mScrolling = true;
                    }else {
                        mScrolling = false;
                    }

                }else {
                    mScrolling = false;
                }

                break;
            case MotionEvent.ACTION_UP:
                mScrolling = false;
                break;
        }
        return mScrolling;
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                return true;//消费触摸事件

            case MotionEvent.ACTION_MOVE:
                x2 = event.getX();
                y2 = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                //左滑
                if (touchDownX - x2 > Utils.dp2px(getContext(), distance)){
                    if(mOnSlideListener!=null){
                        mOnSlideListener.onRightToLeftSlide();
                    }
                }
                //右滑
                if (touchDownX - x2 < -Utils.dp2px(getContext(), distance)) {
                    if(mOnSlideListener!=null){
                        mOnSlideListener.onLeftToRightSlide();
                    }
                }
                //上滑
                if (touchDownY - y2 > Utils.dp2px(getContext(), distance)) {
                    if(mOnSlideListener!=null){
                        mOnSlideListener.onDownToUpSlide();
                    }
                }
                //下滑
                if (touchDownY - y2 < -Utils.dp2px(getContext(), distance)) {
                    if(mOnSlideListener!=null){
                        mOnSlideListener.onUpToDownSlide();
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    private OnSlideListener mOnSlideListener;

    public void setmOnSlideListener(OnSlideListener mOnSlideListener) {
        this.mOnSlideListener = mOnSlideListener;
    }

    public interface OnSlideListener{
        void onRightToLeftSlide();
        void onLeftToRightSlide();
        void onUpToDownSlide();
        void onDownToUpSlide();
    }

转自:https://blog.csdn.net/jiang547860818/article/details/71439706

上一篇下一篇

猜你喜欢

热点阅读