我正在尝试实现 Android ICS 及更高版本中的通知存在的“从左向右滑动以删除”手势。我的应用程序中有一个列表视图。我有手势检测器工作。但是,当我在特定列表项上从左向右滑动时,我希望该项目与我的手指一起移动。当我将手指移动到某个点之外时,只有这样该项目才会自行删除。我的 OnFling() 方法目前还没有做到这一点。
如何修改 OnFling() 方法以使项目与我的手指一起移动?
我的手势监听器类是
class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 25;
private MotionEvent mLastOnDownEvent = null;
@Override
public boolean onDown(MotionEvent e) {
mLastOnDownEvent = e;
return super.onDown(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "On Single TAP up ", Toast.LENGTH_SHORT).show();
return super.onSingleTapUp(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1 == null) {
e1 = mLastOnDownEvent;
}
if (e1 == null || e2 == null) {
return false;
}
float dX = e2.getX() - e1.getX();
float dY = e1.getY() - e2.getY();
if (Math.abs(dY) < SWIPE_MAX_OFF_PATH
&& Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY
&& Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
if (dX > 0) {
int position = tasks.pointToPosition((int) e1.getX(),
(int) e1.getY());
int _id = (int) tasks.getItemIdAtPosition(position);
databaseConnector.deleteContact(_id);
new DeleteRow(_id, contactAdapter, getApplicationContext());
contactAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),
"Right Swipe" + _id, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Left Swipe",
Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}