开发者

Problems with swipe fling gesture and android ListActivity: fling gesture is activating underlying list items

I'm trying to implement a swipe fling gesture on a list activity. This is working but the problem is that the touch events also activate the items on the list!

I've looked for other answers but unfortunately did not find a working solution. Here is the code that I am using:

In the List Activity:

// 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.dis开发者_JAVA技巧patchTouchEvent(ev);

    return handled;
}

And here is the code for my gesture listener:

  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() {};
   }

I am completely stumped. How on earth can I handle gestures in Android without the gesture also activating the underlying items in the list!

P.S. Using view.setOnTouchListener on the root layout resulted in never receiving touch events; that's why I used dispatchTouchEvent.

Any help would be much appreciated.

Thanks!


This isn't a full solution, but I was able to work around the problem with the following code changes:

Instead of using dispatchEvent, I do this:

    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;
        }
    });

I improved the fling gesture as follows:

    @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;
        }
    }

And I removed the onDown() override that was returning true because that was screwing up scrolling on the ListView.

Hope that this can help someone else out there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜