【问题标题】:Problem implementing vertical swipe in Gallery在图库中实现垂直滑动的问题
【发布时间】:2011-03-01 08:05:48
【问题描述】:

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

我想在画廊上实现垂直滑动,我让它工作......有点。我将 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 方法。

关于如何让它发挥作用的任何想法?

【问题讨论】:

    标签: android gallery gesture swipe


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      相关资源
      最近更新 更多