0
我正試圖在列表活動上實現滑動手勢。這是可行的,但問題是觸摸事件也激活列表上的項目!刷卡式滑動手勢和android問題ListActivity:扔手勢正在激活底層列表項
我找過其他答案,但不幸找不到工作解決方案。下面是我使用的代碼:
在列出的活動:
// 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中的手勢,同時激活列表中的基礎項目!
P.S.在根佈局上使用view.setOnTouchListener導致從不接收觸摸事件;這就是我使用dispatchTouchEvent的原因。
任何幫助將不勝感激。
謝謝!