我正在尝试在列表活动上实现滑动投掷手势。这是工作的,但问题是,触摸事件也激活了列表上的项目!
我已经寻找了其他的答案,但不幸的是没有找到一个工作的解决方案。下面是我使用的代码:
在列表活动中:
// 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中,如果手势不激活列表中的底层项目,我该如何处理手势呢?注:使用视图。根布局上的setOnTouchListener导致从未接收到触摸事件;这就是为什么我使用dispatchTouchEvent
任何帮助都将非常感激。
谢谢!
这不是一个完整的解决方案,但我能够通过以下代码更改来解决问题:
不使用dispatchEvent,我这样做:
getListView().setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(final View v, final MotionEvent me)
{
boolean handled = false;
if (mGestureDetector != null)
{
handled = mGestureDetector.onTouchEvent(me);
}
return handled;
}
});
我改进了投掷手势如下:
@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY)
{
if (e1 != null && e2 != null)
{
final float length = Math.abs(e1.getX() - e2.getX());
final float height = Math.abs(e1.getY() - e2.getY());
// Accept criteria: X movement more than or equal to touch slop, total
// slope <= 50%
if (length < mTouchSlop || height / length > 0.50)
{
return false;
}
else
{
if (-velocityX > mMinimumSwipeVelocity)
{
// User swiped finger left.
onSwipeToLeft();
}
else if (velocityX > mMinimumSwipeVelocity)
{
// User swiped finger right.
onSwipeToRight();
}
// We're interested in these series of events
return true;
}
}
else
{
return false;
}
}
我删除了onDown()重写,它返回true因为它搞砸了ListView上的滚动