我正在尝试在列表活动上实现轻扫手势 . 这是有效的,但问题是触摸事件还会激活列表中的项目!
我寻找其他答案,但遗憾的是没有找到有效的解决方案 . 这是我正在使用的代码:
在列表活动中:
// 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.dispatchTouchEvent(ev);
return handled;
}
这是我的手势监听器的代码:
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() {};
}
我完全难过了 . 我怎么能在没有手势的情况下处理Android中的手势,同时激活列表中的基础项目!
附:在根布局上使用view.setOnTouchListener导致从未接收到触摸事件;这就是我使用dispatchTouchEvent的原因 .
任何帮助将非常感激 .
谢谢!
1 回答
这不是一个完整的解决方案,但我能够通过以下代码更改解决此问题:
而不是使用dispatchEvent,我这样做:
我改进了如下手势:
我删除了返回true的onDown()覆盖,因为这搞砸了ListView上的滚动 .
希望这可以帮助其他人 .