我有一个列表视图,用于显示给定日期的条目。列表视图上方有按钮,允许你增加/减少日期。一切正常。我想要的是替换这些按钮,使用户可以向右/向左滑动来增加/减少日期。
最好的方法是什么?我不关心滑动哪个项目,因为对于某个日期可能没有任何列表视图中的项目,只要它发生在列表视图区域内即可。我已经在项目上添加了单击和长按侦听器。
只需实现 OnGestureListener 接口即可。
public class MyListActivity extends ListActivity implements OnGestureListener
使用GestureDetector手势检测器
GestureDetector detector = new GestureDetector(this, this);
将列表的触摸事件传递给GestureDetector
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent e) {
detector.onTouchEvent(e);
return false;
}
});
最后使用fling方法来检测手势。您可以使用速度值来检测移动的方向。
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {}