美文网首页
安卓滑动与点击事件冲突

安卓滑动与点击事件冲突

作者: liuweilyy | 来源:发表于2019-03-22 17:07 被阅读0次

    安卓实现左滑删除的时候滑动与子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

    相关文章

      网友评论

          本文标题:安卓滑动与点击事件冲突

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