我在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方法。
你有什么办法让它工作吗?
发布于 2011-03-01 08:05:49
好了,我找到答案了……onDown()方法需要返回true。对return super.onDown(eve)的调用导致它失败,因为默认实现返回false。
我在StackOverflow上的另一篇文章中找到了答案:
https://stackoverflow.com/questions/5099177
复制相似问题