- int lastX, curX;
- private int totalMove = 0;
- private boolean firstDown = true;//开关
- int duration = 150;
- OnTouchListener listViewOnTouchListener = new OnTouchListener() {
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN: {
- lastX = (int)event.getX();
- totalMove = 0;
- firstDown = false;
-
- return false;
- }
- case MotionEvent.ACTION_MOVE:{
- if (firstDown) {
- curX = (int) event.getX();
- totalMove = 0;
- firstDown = false;
- }
- curX = (int) event.getX();
- int delatX = curX - lastX;
- //if (delatX > 0) {
- totalMove += delatX;
- lastX = curX;
- //}
- return false;
- }
- case MotionEvent.ACTION_UP:{
- boolean result = false;
- if(totalMove > 20 ){
- //
- Log.e("right","right");
- totalMove = 0;
- //things you shouold do here
- result = true;
- }
- if(totalMove < 0 && Math.abs(totalMove) > 20){
- Log.e("left","left");
- totalMove = 0;
- //things you shouold do here
- result= true;
- }
- return result;
- }
- }
- return false;
-
- }
- };
- listView.setOnTouchListener(listViewOnTouchListener );
- 其他的onclickListener onLongClickListener 好普通 无需介绍
- 关键在于onTouchListener中 onDown的时候 返回false onCLICK 与longclick也会感应到这个动作,true就相反。。。然后在onMove的时候去统计移动的距离,设定一个滑动的敏感度,达到这个值就返回false 处理相应的操作,例如划屏更新UI。。。不过这个值就返回true让onclick的事件响应,同时设定一个duration时延给longclick事件 达到某个值的话 也直接返回true。。
|