码迷,mamicode.com
首页 > 移动开发 > 详细

android事件分发的tips

时间:2016-05-07 08:38:09      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

@Override
    public boolean onTouch(View v, final MotionEvent event) {

        CheckableButton cb = (CheckableButton) v;
        if (cb.isEditable()) {
            int action = event.getAction();
            Rect outRect = new Rect();
            getActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
            screenWidth = outRect.width();
            screenHeight = outRect.height();

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    startX = v.getLeft();
                    startY = v.getTop();
                    endX = v.getRight();
                    endY = v.getBottom();
                    Log.i(TAG, "ACTION_DOWN: startX = " + startX);
                    Log.i(TAG, "ACTION_DOWN: startY = " + startY);
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    if(isLongClickModule){
                        int dx = (int) event.getRawX() - lastX;
                        int dy = (int) event.getRawY() - lastY;
                        Log.i(TAG, "onTouch: dx = " + dx);
                        Log.i(TAG, "onTouch: dy = " + dy);
                        int left = v.getLeft() + dx;
                        int top = v.getTop() + dy;
                        int right = v.getRight() + dx;
                        int bottom = v.getBottom() + dy;

                        if (left < 0) {
                            left = 0;
                            right = left + v.getWidth();
                        }
                        if (right > screenWidth) {
                            right = screenWidth;
                            left = right - v.getWidth();
                        }
                        if (top < 0) {
                            top = 0;
                            bottom = top + v.getHeight();
                        }
                        if (bottom > screenHeight) {
                            bottom = screenHeight;
                            top = bottom - v.getHeight();
                        }
                        v.layout(left, top, right, bottom);
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        //与bottomFragment打交道
                        float px = getResources().getDimension(R.dimen.bottomfrag_bar_height);
                        int heightPixels = getActivity().getResources().getDisplayMetrics().heightPixels;
                        if (lastY > heightPixels - px) {
                            mCallbacks.onDragToLoction(getResources().getString(R.string.fa_trash_o));
                            isDeleted = true;
                        }else{
                            mCallbacks.onDragToLoction(getResources().getString(R.string.fa_floppy_o));
                            isDeleted = false;
                        }
                        Log.i(TAG, "onTouch: lastX = " + lastX);
                        Log.i(TAG, "onTouch: lastY = " + lastY);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    v.layout(startX, startY, endX, endY);
                    isLongClickModule = false;
                    if(isDeleted){
                        deleteCb(cb);
                    }
                    Log.i(TAG, "ACTION_UP: ScrollY = " + mScrollView.getScrollY());
                    //timer.cancel();
                    break;
                //当你的手指(或者其它)移动屏幕的时候会触发这个事件,比如当你的手指在屏幕上拖动一个listView或者一个ScrollView而不是去按上面的按钮时会触发这个事件
                //又因为触发是从view树的根部开始,所以在move的时候要加v.getParent().requestDisallowInterceptTouchEvent(true);这句话
                case MotionEvent.ACTION_CANCEL:
                    isLongClickModule = false;
                    v.layout(startX, startY, endX, endY);
                    if(isDeleted){
                        deleteCb(cb);
                    }
                    //timer.cancel();
                    break;
            }

        }
        return false;
    }

1.当ontouch return false的时候onTouch执行后会继续执行Onclick,onLongClick之类的函数,return true时就不会执行了,所以ACTION_DOWN的优先级是高于click的
2.事件的派发总是自顶向下即先父布局在子布局,如果不想让父布局接受事件需要在恰当的位置添加v.getParent().requestDisallowInterceptTouchEvent(true);这样一句话
3.当你的手指(或者其它)移动屏幕的时候会触发这个事件,比如当你的手指在屏幕上拖动一个listView或者一个ScrollView而不是去按上面的按钮时会触发这个事件。

android事件分发的tips

标签:

原文地址:http://blog.csdn.net/u013260304/article/details/51331413

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!