开发者

Problem implementing vertical swipe in Gallery

开发者 https://www.devze.com 2023-02-13 06:02 出处:网络
I a开发者_如何学Pythonsked a similar question in the android developers group but haven\'t received a response yet, so I figured I\'d try my luck here.

I a开发者_如何学Pythonsked a similar question in the android developers group but haven't received a response yet, so I figured I'd try my luck here.

I want to implement a vertical swipe on a Gallery and I have it working... sort of. I subclassed Gallery so that I could override the onFling and onDown methods.

Here is the code I used to override these methods:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
    if (m_curTouchPos == NO_CURRENT_TOUCH_POS || m_callback == null)
        return super.onFling(e1, e2, velocityX, velocityY);

    float e1x = e1.getX();
    float e1y = e1.getY();
    float e2x = e2.getX();
    float e2y = e2.getY();

    float offPath = Math.abs(e1x - e2x);
    float distance = Math.abs(e1y - e2y);

    if (offPath < s_swipeMaxOffPath && 
        //Math.abs(velocityY) >= s_swipeMinVelocity && 
        distance >= s_swipeMinDistance)
    {
        if (e1y > e2y)
        {
            m_callback.onSwipeUp(m_curTouchPos);
            //return true;
        }
        else if (e2y > e1y)
        {
            //TODO: IMPLEMENT THIS
            //m_callback.onSwipeDown(m_curTouchPos);
            //return true;
        }
    }

    m_curTouchPos = NO_CURRENT_TOUCH_POS;
    return super.onFling(e1, e2, velocityX, velocityY);
}

@Override
public boolean onDown(MotionEvent eve)
{
    m_curTouchPos = pointToPosition((int)eve.getX(), (int)eve.getY());
    return super.onDown(eve);
}

The problem is that onFling doesn't get called when I do a vertical swipe... In order to get into the onFling method I have to press on an item in the gallery, slide it slowly a little to the left or right, and then swipe vertically.

Horizontal swipes always get into the onFling method.

Any ideas on how to get this to work?


Ok, I have found the answer... The onDown() method needs to return true. The call to return super.onDown(eve) was causing it to fail because the default implementation returns false.

I found the answer in another post here on StackOverflow:

Android: GestureDetector not working (gestureDetector.onTouchEvent(event) always false) with Tabs (TabActivity, Tabwidget)

0

精彩评论

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