android:在触摸移动时移动视图:有时ACTION_MOVE不会到来

时间:2014-09-18 15:14:49

标签: android

我正在实施拉动关闭功能。如果用户拉出当前布局,则它将设置动画,然后关闭屏幕。但有时候我没有得到ACTION_MOVE事件,应用程序无法检查是否发生任何拉动事件。

PFB my code snippet

public boolean onTouchEvent(MotionEvent event) {

    if (event.getActionMasked() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
        return false;
    }
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_MOVE:
            if (mIsDragged) {
                lastYPosition= event.getY();
                lastXPosition = event.getX();
                return true;
            }
            else{
                int diffY = (int)(event.getY() - lastYPosition);
                int diffX = (int)(event.getX() - lastXPosition);

                //Check if the action was Pull
                if(diffY<0 && (Math.abs(diffY) > Math.abs(diffX)) && Math.abs(diffY) > mTouchSlop){
                    mIsDragged = true;

                    TranslateAnimation anim = new TranslateAnimation(0, 0, 50, 50);
                    anim.setFillAfter(true);
                    anim.setDuration(400);
                    anim.setInterpolator(new AccelerateDecelerateInterpolator());
                    draggedLayout.setVisibility(View.VISIBLE);
                    anim.setAnimationListener(new AnimationListener() {

                        @Override
                        public void onAnimationStart(Animation animation) { }

                        @Override
                        public void onAnimationRepeat(Animation animation) { }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            ((Activity)mContext).finish();
                            ((Activity)mContext).overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
                        }
                    });
                    draggedLayout.startAnimation(anim);
                }
                else{
                    mIsDragged = false;
                }
                lastYPosition = event.getY();
                lastXPosition = event.getX();
                return true;
            }
        case MotionEvent.ACTION_DOWN:
            lastYPosition = event.getY();
            lastXPosition = event.getX();
            mIsDragged = false;
            return true;
    }

    return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN && !mIsDragged){
        lastXPosition = event.getX();
        lastYPosition = event.getY();
        mIsDragged = false;
    }           
  //        return mIsDragged;

    return true;
}

1 个答案:

答案 0 :(得分:0)

可能是因为你在onTouchEvent中返回false:

if (event.getActionMasked() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
    return false;
}

如果事件返​​回false,系统会将事件传播到其他视图以查看它们是否会处理此事件。

onInterceptTouchEvent()的文档包含有关如何实现onTouchEvent()以及返回内容的规则:

For OnInterceptTouchEvent():

2) The down event will be handled either by a child of this view group, or given to your own 
onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, 
so you will continue to see the rest of the gesture (instead of looking for a parent view to 
handle it). Also, by returning true from onTouchEvent(), you will not receive any following 
events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like 
normal.

http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)