在Gallery中实现垂直滑动的问题

时间:2011-02-24 00:47:51

标签: android gallery gesture swipe

我在android开发者小组中问了一个类似的问题,但还没有收到回复,所以我想我在这里试试运气。

我想在Gallery上实现垂直滑动,我让它工作......有点儿。我将Gallery子类化,以便我可以覆盖onFling和onDown方法。

以下是我用来覆盖这些方法的代码:

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

问题是当我进行垂直滑动时onFling不会被调用...为了进入onFling方法,我必须按下图库中的项目,将它慢慢滑到左边或者向右,然后垂直滑动。

水平滑动总是进入onFling方法。

有关如何使其发挥作用的任何想法?

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案...... onDown()方法需要返回true。返回super.onDown(eve)的调用导致它失败,因为默认实现返回false。

我在StackOverflow上的另一篇文章中找到了答案:

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