[滑动冲突处理实战]让子view响应点击事件,滑动交给父view

有一个这样的场景,一个音乐条 包裹 ,音乐条里面有一个头像,点击头像可以展开收缩条,但是 触摸头像的时候还允许滑动,那么写了几次代码解决了问题,但是没法解决一个按下去然后拖动 刚开始会跳动的问题

写法就是自定义viewgroup重写onInterceptTouchEvent方法
判断点击距离,如果判断距离没超过3dp就返回true,下发给view,否则返回true,
child不需要写任何处理代码

    public boolean onInterceptTouchEvent(MotionEvent event) {//好像只在down的时候触发
        final float scale = getContext().getResources().getDisplayMetrics().density;//density=dpi/160
        int clickDistance = (int) (3 * scale + 0.5f);
        int actionMasked = event.getActionMasked();
        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
                hasMove = false;
                downXDispatch = event.getX();
                downYDispatch = event.getY();
/*
                downXDispatch = event.getRawX();
                downYDispatch = event.getRawY();
*/
                Prt.w(TAG, "onInterceptTouchEvent down->" + downYDispatch + "," + event.getY());
                return false;//首先子类优先处理 点击事件,
            case MotionEvent.ACTION_MOVE:
                float distanceX = event.getX() - downXDispatch;//表示触摸到哪里,但是 你滑动具体是多少是得转换的
                float distanceY = event.getY() - downYDispatch;
                if (hasMove == false && Math.abs(distanceX) <= clickDistance && Math.abs(distanceY) <= clickDistance) {
                    Prt.w(TAG, "onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:" + distanceX + ",distancey:" + distanceY + ",点击距离:" + clickDistance + "," + ",downY:" + downYDispatch + ",downX:" + downXDispatch);
                    return false;

                } else {
                    Prt.w(TAG, "onInterceptTouchEvent 触摸事件,交给自己处理");
                    return true;//交给自己处理
                }

            case MotionEvent.ACTION_UP:
                if (hasMove) {
                    return false;
                }
                break;


        }
        return super.onInterceptTouchEvent(event);
    }

    private float downXDispatch;
    private float downYDispatch;
    float downX = 0;
    float downY = 0;

    //            public boolean onTouch(View v, MotionEvent event) {
    public boolean onTouchEvent(MotionEvent event) {
        int actionMasked = event.getActionMasked();
        switch (actionMasked) {
//                switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                        pressTime = System.currentTimeMillis();
                downX = event.getRawX();
                downY = event.getRawY();
                Prt.w(TAG, "onTouchEvent ACTION_DOWN");
                return true;
            case MotionEvent.ACTION_MOVE:

                float distanceX = event.getRawX() - downX;//表示触摸到哪里,但是 你滑动具体是多少是得转换的
                float distanceY = event.getRawY() - downY;

                downX = event.getRawX();
                downY = event.getRawY();


                float translationX = this.getTranslationX() + distanceX;
                float translationY = this.getTranslationY() + distanceY;
                if (onMoveListener != null) {
                    onMoveListener.onTransMove(translationX, translationY);
                }
                Prt.w(TAG, "onTouchEvent ACTION_MOVE,will settransX:" + translationX + ",will transY:" + translationY + ",beforeTranslationX:" + this.getTranslationX() + ",transY:" + this.getTranslationY() + ",distnceX:" + distanceX + ",distanceY:" + distanceY);
                this.setTranslationX(translationX);
                this.setTranslationY(translationY);
           /*     offsetLeftAndRight((int) translationX);
                offsetTopAndBottom((int) translationY);*/
                return true;
            case MotionEvent.ACTION_UP:
                Prt.w(TAG, "onTouchEvent ACTION_UP");
            default:
                return false;
        }
    }
//        });


//    }

    interface OnMoveListener {
        void onTransMove(float x, float y);
    }

    public void setOnMoveListener(OnMoveListener onMoveListener) {
        this.onMoveListener = onMoveListener;
    }

    OnMoveListener onMoveListener;
o.tv W/WidthAnimLinearView: onTouchEvent ACTION_UP
03-28 18:02:22.438 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent down->81.0,81.0
03-28 18:02:22.459 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:0.0,distancey:0.0,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.476 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:0.58836365,distancey:0.0,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.492 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:3.1565247,distancey:0.5782471,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.509 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:8.330505,distancey:2.5826416,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.525 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 触摸事件,交给自己处理
03-28 18:02:22.542 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:-99.310486,will transY:21.172363,beforeTranslationX:0.0,transY:0.0,distnceX:-99.310486,distanceY:21.172363
03-28 18:02:22.558 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:16.042633,will transY:2.4191895,beforeTranslationX:0.0,transY:0.0,distnceX:16.042633,distanceY:2.4191895
03-28 18:02:22.575 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:12.2222595,will transY:0.40844727,beforeTranslationX:0.0,transY:0.0,distnceX:12.2222595,distanceY:0.40844727
03-28 18:02:22.591 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:11.5729065,will transY:1.0,beforeTranslationX:0.0,transY:0.0,distnceX:11.5729065,distanceY:1.0
03-28 18:02:22.608 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:11.417755,will transY:0.58898926,beforeTranslationX:0.0,transY:0.0,distnceX:11.417755,distanceY:0.58898926
03-28 18:02:22.624 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.368561,will transY:0.41101074,beforeTranslationX:0.0,transY:0.0,distnceX:8.368561,distanceY:0.41101074
03-28 18:02:22.641 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:9.164856,will transY:1.5797119,beforeTranslationX:0.0,transY:0.0,distnceX:9.164856,distanceY:1.5797119
03-28 18:02:22.657 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:9.862762,will transY:0.4202881,beforeTranslationX:0.0,transY:0.0,distnceX:9.862762,distanceY:0.4202881
03-28 18:02:22.674 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:10.573273,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:10.573273,distanceY:0.0
03-28 18:02:22.690 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.449524,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:8.449524,distanceY:0.0
03-28 18:02:22.707 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.404022,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:8.404022,distanceY:0.0
03-28 18:02:22.723 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.392334,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.392334,distanceY:0.0
03-28 18:02:22.740 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.5820007,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.5820007,distanceY:0.0
03-28 18:02:22.756 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.986145,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.986145,distanceY:0.0
03-28 18:02:22.773 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:6.9989014,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:6.9989014,distanceY:0.0
03-28 18:02:22.789 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:7.58667,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:7.58667,distanceY:-1.0
03-28 18:02:22.806 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.990906,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.990906,distanceY:-1.0
03-28 18:02:22.822 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:9.007904,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:9.007904,distanceY:-1.0
03-28 18:02:22.839 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:7.448944,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:7.448944,distanceY:0.0
03-28 18:02:22.855 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:6.0026855,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:6.0026855,distanceY:-1.0
03-28 18:02:22.872 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:6.001953,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:6.001953,distanceY:0.0
03-28 18:02:22.888 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.967499,will transY:-0.5780029,beforeTranslationX:0.0,transY:0.0,distnceX:5.967499,distanceY:-0.5780029
03-28 18:02:22.905 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.717407,will transY:-0.9971924,beforeTranslationX:0.0,transY:0.0,distnceX:8.717407,distanceY:-0.9971924
03-28 18:02:22.921 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:10.924286,will transY:-1.4248047,beforeTranslationX:0.0,transY:0.0,distnceX:10.924286,distanceY:-1.4248047
03-28 18:02:22.936 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:11.624298,will transY:-3.0,beforeTranslationX:0.0,transY:0.0,distnceX:11.624298,distanceY:-3.0

从上面的第一个onTouchEvent可以看出来会有一个bug,刚按下去的时候导致view发生超大的移动
这个改成获取屏幕的坐标就ok.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342