未调用MotionEvent.ACTION_UP

时间:2013-04-03 23:10:07

标签: android listview

请考虑以下方案(为了更好地理解我的问题)。 enter image description here

如您所见,我正在考虑用填充包围的列表视图。现在,如果用户按下listview项,我提供的动作为浅蓝色背景色。现在,我的应用程序正在处理onTouch事件本身以确定

之类的操作
  • 点击
  • 从左向右滑动
  • 从右向左滑动

这是我的代码。

public boolean onTouch(View v, MotionEvent event) {
        if(v == null)
        {
            mSwipeDetected = Action.None;
            return false;
        }
        switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getRawX();
            downY = event.getRawY();
            mSwipeDetected = Action.Start;

         // Find the child view that was touched (perform a hit test)
            Rect rect = new Rect();
            int childCount = listView.getChildCount();
            int[] listViewCoords = new int[2];
            listView.getLocationOnScreen(listViewCoords);
            int x = (int) event.getRawX() - listViewCoords[0];
            int y = (int) event.getRawY() - listViewCoords[1];
            View child;
            for (int i = 0; i < childCount; i++) {
                child = listView.getChildAt(i);
                child.getHitRect(rect);
                if (rect.contains(x, y)) {
                    mDownView = child;
                    break;
                }
            }


            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE: {
            upX = event.getRawX();
            upY = event.getRawY();
            float deltaX=0,deltaY=0;
             deltaX = downX - upX;
             deltaY = downY - upY;

                if(deltaY < VERTICAL_MIN_DISTANCE)
                {
                            setTranslationX(mDownView, -(deltaX));
                            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / listView.getWidth())));
                            return false;
                }
                else
                {
                    forceBringBack(v);
                }

                          return false;              

        }
        case MotionEvent.ACTION_UP:
        {

             stopX = event.getX();
             float stopValueY = event.getRawY() - downY;             
             float stopValue = stopX - downX;

             if(!mDownView.isPressed())
             {
                 forceBringBack(mDownView);
                 return false;
             }             

             boolean dismiss = false;
             boolean dismissRight = false;


             if(Math.abs(stopValue)<10)
             {
                 mSwipeDetected = Action.Start;
             }
             else
             {
                 mSwipeDetected = Action.None;

             }
             String log = "";
             Log.d(log, "Here is Y" + Math.abs(stopValueY));
             Log.d(log, "First Comparison of Stop Value > with/4" + (Math.abs(stopValue) > (listView.getWidth() /4)));
             Log.d(log, "Second Comparison " + (Math.abs(stopValueY)<VERTICAL_MIN_DISTANCE));
             Log.d(log, "Action Detected is " + mSwipeDetected + " with Stop Value  " + stopValue);

             if((Math.abs(stopValue) > (listView.getWidth() /4))&&(Math.abs(stopValueY)<VERTICAL_MIN_DISTANCE))
             {
                 dismiss = true;
                 dismissRight = stopValue > 0;

                 if(stopValue>0)
                 {
                 mSwipeDetected = Action.LR;

                 }
                 else
                     mSwipeDetected = Action.RL;
             }
             Log.d(log, "Action Detected is " + mSwipeDetected + " with Stop Value after dissmiss" + stopValue);

             if(dismiss)
             {
                 if(dismissRight)
                     mSwipeDetected = Action.LR;
                 else
                     mSwipeDetected = Action.RL;
                 animate(mDownView)
                 .translationX(dismissRight ? listView.getWidth() : - listView.getWidth())
                 .alpha(0)
                 .setDuration(mAnimationTime)
                 .setListener(new AnimatorListenerAdapter() {
                     public void onAnimationEnd(Animator animation)
                     {

                     }
                });
             }
             else
             {
                 animate(mDownView)
                 .translationX(0)
                 .alpha(1)
                 .setDuration(mAnimationTime)
                 .setListener(null);
             }


             break;           

        }
        }
        return false;
    }

如您所见,我在MotionEvent.ACTION_UP中确定已执行的操作,并相应地设置Enum Action的值。如果用户没有越过列表视图边界,则此逻辑就像魅力一样。

现在,如果用户在滑动(或特定)时,沿着列表项移动他的手指从蓝色移动到橙色,则MotionEvent.ACTION_UP将不会被提供给listview,这会导致我的代码不做出决定,由于translateX()方法和setAlpha(),由于在这种情况下没有确定Action,因此该特定列表项变为空白。

问题并没有在这里停止,因为,我不是每次都在膨胀视图,每次导致多次出现空白/白色列表项时,相同的translatedX()行会被充气。

有没有可能这样做,即使我没有遇到MotionEvent.ACTION_UP,我仍然可以做出一些决定?

4 个答案:

答案 0 :(得分:187)

您应该return true; case MotionEvent.ACTION_DOWN:,因此MotionEvent.ACTION_UP将被处理。


正如View.OnTouchListener所述:

  

<强>返回

     

如果监听器已经使用了该事件,则为true,否则为false。

MotionEvent.ACTION_UPMotionEvent.ACTION_DOWN发生之前不会被调用,对此的合理解释是,如果ACTION_UP之前从未发生ACTION_DOWN,则不可能发生ACTION_DOWN

此逻辑使开发人员能够在{{1}}之后阻止更多事件。

答案 1 :(得分:20)

另请注意,在某些情况下(例如屏幕旋转),手势可能会被取消,在这种情况下,不会发送MotionEvent.ACTION_UP。相反,会发送MotionEvent.ACTION_CANCEL。因此,正常的操作开关语句应如下所示:

switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        // check if we want to handle touch events, return true
        // else don't handle further touch events, return false
    break;

    // ... handle other cases

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        // finish handling touch events
        // note that these methods won't be called if 'false' was returned
        // from any previous events related to the gesture
    break;
}

答案 2 :(得分:6)

我不认为将return true;添加到案例MotionEvent.ACTION_DOWN:最终会解决问题。这只会使return false能够像魅力一样完成工作的情况变得复杂。

需要注意的是:MotionEvent.ACTION_DOWN: /*something*/ return true;将阻止可用于视图的任何其他侦听器回调,即使是onClickListenerm,而return false中的MotionEvent.ACTION_UP:正确可以帮助将MotionEvent传播到正确的目的地

参考他原来的代码:https://github.com/romannurik/android-swipetodismiss

答案 3 :(得分:0)

作为Danpe explained in his concise answer - 我必须添加ACTION_DOWN代码才能识别ACTION_UP。

            case MotionEvent.ACTION_DOWN:

                return true;

            case MotionEvent.ACTION_UP:

                XyPos xyPos = new XyPos();
                xyPos.x = last_x;
                xyPos.y = last_y;
                handleViewElementPositionUpdate(xyPos);

                break;

无论如何我让整个onTouch(..)方法返回true,所以我不确定为什么这还不够......但很高兴有这个快速解决方案..(谢谢!)