安卓爬坑系列之事件分发机制

2018-11-02  本文已影响0人  autisticBoy

发现问题

前几天想做一个外面viewPager里面嵌套一个横向得recyclerview的效果,选择用外部拦截法去完成。先自定义了一个horizonViewPager,(里面还没有加recyclerview)首先重写了onInterceptTouchEvent方法,代码如下:

public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercepted = false;
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        final int action = ev.getAction() & MotionEvent.ACTION_MASK;
        switch (action) {
            case ACTION_DOWN: {
                intercepted = false;
                Log.d("luchixiang", "onInterceptTouchEvent: "+"down");
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                /*int count = getChildCount();
                View child;
                child = getChildAt(0).findViewById(R.id.luchixaing4);
                int[] location = new int[2];
                child.getLocationOnScreen(location);*/
                Log.d("luchixiang", "onInterceptTouchEvent: " + 1);
                intercepted =false;
//1                if (y >= location[1] && y < location[1] + child.getHeight()) {
//                    intercepted = false;
//                } else intercepted = true;
            }
            case MotionEvent.ACTION_UP: {
                intercepted = false;
                break;
            }
        }
        return intercept;
    }

一段十分简单的代码,但是在滑动的时候里面的日志没有打印出来,也就是在move动作的时候没有经过onInterceptTouchEvent这个方法

问题所在

首先在明确下面几点

解决问题

再里面放进去recyclerview(有那么一点坑),不过借此了解了事件分发机制,这波不亏

上一篇 下一篇

猜你喜欢

热点阅读