OnTouch和OnLong点击相同的视图

时间:2013-11-23 18:40:53

标签: java android onclick onlongclicklistener

我有一个使用OnClickListener移动视图的程序:

       iv.setOnTouchListener(new View.OnTouchListener() {
            public boolean  onTouch(View v,MotionEvent event) {


                iAction = event.getActionMasked();
                switch (iAction) {
                case MotionEvent.ACTION_MOVE:
                    v.getLocationOnScreen(aLocation);                   // get absolute physical location of this View
                    iOffsetX =  (aLocation [ 0 ] - (int) v.getX());     // subtract out this View's relative location within its parent View...
                    iOffsetY =  (aLocation [ 1 ] - (int) v.getY());     // ...yielding the offsets that convert getRawX/Y's coords to setX/Y's coords

                    iNewX = (int) event.getRawX();                      // get absolute physical coords of this touch
                    iNewY = (int) event.getRawY();
                    iNewX -= iOffsetX;                                  // remove parent View's screen offset (calc'd above)
                    iNewY -= iOffsetY;
                    iNewX -= iRelX;                                     // remove stored touch offset
                    iNewY -= iRelY;
                    v.setX(iNewX);                                      // finally, move View to new coords (relative to its parent View)
                    v.setY(iNewY);
                    bExitValue = true;

                break;

                case MotionEvent.ACTION_DOWN:
                    if (delete){ v.setVisibility(View.GONE); }
                    iRelX = (int) event.getX();                         // preserve offset of this touch within the View
                    iRelY = (int) event.getY();
                    bExitValue = false;
                break;

                case MotionEvent.ACTION_UP:
                    bExitValue = true;
                break;

                }
                return (bExitValue);


            }
        });

这样可以正常工作,在该代码中,如果View被移动,我试图返回true,因此OnLongClick不会被调用。问题是我只想返回true,如果它被移动,所以在ACTION_DOWN中它返回false,因为第一次返回false,OnLongClick也会在移动视图时被调用。

我尝试在ACTION_MOVE中使用v.setLongClickable(false),但它也是如此。

有没有办法在OnLongClick正在进行时中断?

0 个答案:

没有答案
相关问题