滑动手势和android ListActivity的问题:fling手势正在激活基础列表项

时间:2011-08-24 21:18:40

标签: android listactivity gestures

我正在尝试在列表活动上实现轻扫手势。这是有效的,但问题是触摸事件还会激活列表中的项目!

我已经寻找其他答案,但遗憾的是没有找到可行的解决方案。这是我正在使用的代码:

在列表活动中:

// Setup the swipe detector
mSwipeGestureListener = new SwipeGestureListener(this);
mGestureDetector = new GestureDetector(mSwipeGestureListener);
mGestureDetector.setIsLongpressEnabled(false);

@Override
public boolean dispatchTouchEvent(final MotionEvent ev)
{
    boolean handled = false;

    if (mGestureDetector != null)
    {
        handled = mGestureDetector.onTouchEvent(ev);
    }

    handled |= super.dispatchTouchEvent(ev);

    return handled;
}

这是我的手势监听器的代码:

  public class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener
  {
    protected final Activity mActivity;
    protected final int mTouchSlop;
    protected final int mMinimumSwipeVelocity;

    public SwipeGestureListener(final Activity activity)
    {
        mActivity = activity;
        final ViewConfiguration viewConfiguration = ViewConfiguration.get(activity);

        mTouchSlop = viewConfiguration.getScaledTouchSlop();
        mMinimumSwipeVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    }

    @Override
    public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY)
    {
        // Don't handle swipe if Y offset is greater than touch slop
        if (Math.abs(e1.getY() - e2.getY()) > mTouchSlop)
        {
            return false;
        }
        else
        {
            if (e1.getX() - e2.getX() > mTouchSlop
             && -velocityX > mMinimumSwipeVelocity)
            {
                // User swiped finger left.
                onSwipeToLeft();
            }
            else if (e2.getX() - e1.getX() > mTouchSlop
             && velocityX > mMinimumSwipeVelocity)
            {
                // User swiped finger left.
                onSwipeToRight();
            }

            // We're interested in these series of events
            return true;
        }        
    }

    // It is necessary to return true from onDown for the onFling event to register
    @Override
    public boolean onDown(final MotionEvent e)
    {
        return true;
    }

    protected void onSwipeToLeft() {}

    protected void onSwipeToRight() {};
   }

我完全难过了。我怎么能在没有手势的情况下处理Android中的手势,同时激活列表中的基础项目!

P.S。在根布局上使用view.setOnTouchListener导致从未接收到触摸事件;这就是我使用dispatchTouchEvent。

的原因

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

这不是一个完整的解决方案,但我能够通过以下代码更改来解决问题:

我没有使用dispatchEvent,而是这样做:

    getListView().setOnTouchListener(new View.OnTouchListener() 
    {
        @Override
        public boolean onTouch(final View v, final MotionEvent me)
        {
            boolean handled = false;

            if (mGestureDetector != null)
            {
                handled = mGestureDetector.onTouchEvent(me);
            }

            return handled;
        }
    });

我改进了如下手势:

    @Override
    public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY)
    {        
        if (e1 != null && e2 != null)
        {
            final float length = Math.abs(e1.getX() - e2.getX());
            final float height = Math.abs(e1.getY() - e2.getY());

            // Accept criteria: X movement more than or equal to touch slop, total
            // slope <= 50%
            if (length < mTouchSlop || height / length > 0.50)
            {
                return false;
            }
            else
            {
                if (-velocityX > mMinimumSwipeVelocity)
                {
                    // User swiped finger left.
                    onSwipeToLeft();
                }
                else if (velocityX > mMinimumSwipeVelocity)
                {
                    // User swiped finger right.
                    onSwipeToRight();
                }

                // We're interested in these series of events
                return true;
            }
        }
        else
        {
            return false;
        }
    }

我删除了返回true的onDown()覆盖,因为这搞砸了ListView上的滚动。

希望这可以帮助其他人。