我有以下列表活动:
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;
}
}
}
列表项上的ContextListener(长单击)工作得很好。今天我添加了GestureListener和Detector,它也可以工作,但是:
GestureDetector可以检测到一个fling,但是在完成逻辑之后,上下文菜单就会打开,这显然不是我想要的。你知道我做错了什么吗?或者我该怎么办?
最佳答案
通常,在检测到onfling之后,需要为视图及其所有子视图调用cancellongpress()方法。但是对于AbslistView,此方法不执行任何操作:(
但我找到了解决方法:在onfling回调函数中,处理完所有事情后,对listview对象执行下一步:
Event cancelEvent = Event.obtain(ev2);
cancelEvent.setAction(MotionEvent.ACTION_UP);
listView.onTouchEvent(cancelEvent);