我有以下ListActivity:
I have a the following ListActivity:
public class ShowDayActivity extends ListActivity implements OnItemClickListener {
private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.day);
registerForContextMenu(getListView());
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
getListView().setOnItemClickListener(this);
getListView().setOnTouchListener(gestureListener);
}
@SuppressWarnings("static-access")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
...
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
...
return super.onContextItemSelected(item);
}
Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
editEvent(pos);
}
class MyGestureDetector extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("ICS-Calendar", "Fling left");
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("ICS-Calendar", "Fling right");
return true;
}
return false;
}
}
}
在listItems中的contextListener(长按)完美的作品。今天,我加入了gestureListener和检测器,它的工作原理也一样,但是:
The contextListener (long-click) on the listItems works perfectly. Today i added the gestureListener and Detector, which works too, BUT:
该GestureDetector检测一扔所有的权利,但在它与它的逻辑做的,上下文菜单打开,这显然不是我想要的。任何想法,我做错了,或者我可以做什么呢?
The GestureDetector detects a fling all right, but after it's done with its logic, the context menu opens, which is obviously not what i want. Any ideas what i'm doing wrong, or what i might do about it?
通常情况下,你需要调用的 cancelLong preSS()的方法查看毕竟视图的孩子的 onFling 的已被检测到。但对于 AbsListView 的这个方法做什么:(
Normally, you need to call cancelLongPress() method for view and all view's children after onFling has been detected. But for AbsListView this method do nothing :(
不过,我已经找到解决方法是:在 onFling 的回调,毕竟事情进行了处理,做下为您的的ListView 的对象:
But I've found workaround for it: in the onFling callback, after all things were processed, do the next for your ListView object:
Event cancelEvent = Event.obtain(ev2);
cancelEvent.setAction(MotionEvent.ACTION_UP);
listView.onTouchEvent(cancelEvent);